Skip to content

Commit e6d2dad

Browse files
shmaxkoenpunt
authored andcommitted
fix eager loading caching
1 parent 5ec3bad commit e6d2dad

File tree

8 files changed

+22
-8
lines changed

8 files changed

+22
-8
lines changed

lib/Model.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,7 @@ public static function find(/* $type, $options */)
16161616
* @param mixed $pks primary keys
16171617
* @return array
16181618
*/
1619-
protected static function get_models_from_cache($pks)
1619+
protected static function get_models_from_cache($pks, $options)
16201620
{
16211621
$models = array();
16221622
$table = static::table();
@@ -1628,7 +1628,7 @@ protected static function get_models_from_cache($pks)
16281628

16291629
foreach($pks as $pk)
16301630
{
1631-
$options = array('conditions' => static::pk_conditions($pk));
1631+
$options['conditions'] = static::pk_conditions($pk);
16321632
$models[] = Cache::get($table->cache_key_for_model($pk), function() use ($table, $options)
16331633
{
16341634
$res = $table->find($options);
@@ -1658,7 +1658,7 @@ public static function find_by_pk($values, $options)
16581658

16591659
if($table->cache_individual_model)
16601660
{
1661-
$list = static::get_models_from_cache($values);
1661+
$list = static::get_models_from_cache($values, $options);
16621662
}
16631663
else
16641664
{

test/RelationshipTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,12 @@ public function test_eager_loading_has_many_x()
563563
$this->assert_equals(2, count($venues[0]->events));
564564
}
565565

566+
public function test_eager_loading_has_many_x_with_caching()
567+
{
568+
Publisher::find(array(1, 2, 3), array('include' => 'authors'));
569+
$this->assert_sql_has("WHERE publisher_id IN(?)",ActiveRecord\Table::load('Author')->last_sql);
570+
}
571+
566572
public function test_eager_loading_has_many_with_no_related_rows()
567573
{
568574
$venues = Venue::find(array(7, 8), array('include' => 'events'));

test/fixtures/authors.csv

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
author_id,parent_author_id,name
2-
1,3,"Tito"
3-
2,2,"George W. Bush"
4-
3,1,"Bill Clinton"
5-
4,2,"Uncle Bob"
1+
author_id,parent_author_id,name,publisher_id
2+
1,3,"Tito",1
3+
2,2,"George W. Bush",1
4+
3,1,"Bill Clinton",2
5+
4,2,"Uncle Bob",3

test/models/Publisher.php

+4
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ class Publisher extends ActiveRecord\Model
44
static $pk = 'publisher_id';
55
static $cache = true;
66
static $cache_expire = 2592000; // 1 month. 60 * 60 * 24 * 30
7+
8+
static $has_many = array(
9+
'authors'
10+
);
711
}

test/sql/mysql.sql

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CREATE TABLE authors(
22
author_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
33
parent_author_id INT,
4+
publisher_id INT,
45
name VARCHAR(25) NOT NULL DEFAULT 'default_name',
56
updated_at datetime,
67
created_at datetime,

test/sql/oci.sql

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ CREATE SEQUENCE authors_seq;
22
CREATE TABLE authors(
33
author_id INT NOT NULL PRIMARY KEY,
44
parent_author_id INT,
5+
publisher_id INT,
56
name VARCHAR(25) DEFAULT 'default_name' NOT NULL,
67
updated_at timestamp,
78
created_at timestamp,

test/sql/pgsql.sql

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CREATE TABLE authors(
22
author_id SERIAL PRIMARY KEY,
33
parent_author_id INT,
4+
publisher_id INT,
45
name VARCHAR(25) NOT NULL DEFAULT 'default_name',
56
updated_at timestamp,
67
created_at timestamp,

test/sql/sqlite.sql

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CREATE TABLE authors(
22
author_id INTEGER NOT NULL PRIMARY KEY,
33
parent_author_id INT,
4+
publisher_id INT,
45
name VARCHAR (25) NOT NULL DEFAULT default_name, -- don't touch those spaces
56
updated_at datetime,
67
created_at datetime,

0 commit comments

Comments
 (0)