Skip to content

Commit

Permalink
fix eager loading caching
Browse files Browse the repository at this point in the history
  • Loading branch information
shmax authored and koenpunt committed Jun 11, 2017
1 parent 5ec3bad commit e6d2dad
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 8 deletions.
6 changes: 3 additions & 3 deletions lib/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,7 @@ public static function find(/* $type, $options */)
* @param mixed $pks primary keys
* @return array
*/
protected static function get_models_from_cache($pks)
protected static function get_models_from_cache($pks, $options)
{
$models = array();
$table = static::table();
Expand All @@ -1628,7 +1628,7 @@ protected static function get_models_from_cache($pks)

foreach($pks as $pk)
{
$options = array('conditions' => static::pk_conditions($pk));
$options['conditions'] = static::pk_conditions($pk);
$models[] = Cache::get($table->cache_key_for_model($pk), function() use ($table, $options)
{
$res = $table->find($options);
Expand Down Expand Up @@ -1658,7 +1658,7 @@ public static function find_by_pk($values, $options)

if($table->cache_individual_model)
{
$list = static::get_models_from_cache($values);
$list = static::get_models_from_cache($values, $options);
}
else
{
Expand Down
6 changes: 6 additions & 0 deletions test/RelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,12 @@ public function test_eager_loading_has_many_x()
$this->assert_equals(2, count($venues[0]->events));
}

public function test_eager_loading_has_many_x_with_caching()
{
Publisher::find(array(1, 2, 3), array('include' => 'authors'));
$this->assert_sql_has("WHERE publisher_id IN(?)",ActiveRecord\Table::load('Author')->last_sql);
}

public function test_eager_loading_has_many_with_no_related_rows()
{
$venues = Venue::find(array(7, 8), array('include' => 'events'));
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/authors.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
author_id,parent_author_id,name
1,3,"Tito"
2,2,"George W. Bush"
3,1,"Bill Clinton"
4,2,"Uncle Bob"
author_id,parent_author_id,name,publisher_id
1,3,"Tito",1
2,2,"George W. Bush",1
3,1,"Bill Clinton",2
4,2,"Uncle Bob",3
4 changes: 4 additions & 0 deletions test/models/Publisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ class Publisher extends ActiveRecord\Model
static $pk = 'publisher_id';
static $cache = true;
static $cache_expire = 2592000; // 1 month. 60 * 60 * 24 * 30

static $has_many = array(
'authors'
);
}
1 change: 1 addition & 0 deletions test/sql/mysql.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CREATE TABLE authors(
author_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
parent_author_id INT,
publisher_id INT,
name VARCHAR(25) NOT NULL DEFAULT 'default_name',
updated_at datetime,
created_at datetime,
Expand Down
1 change: 1 addition & 0 deletions test/sql/oci.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ CREATE SEQUENCE authors_seq;
CREATE TABLE authors(
author_id INT NOT NULL PRIMARY KEY,
parent_author_id INT,
publisher_id INT,
name VARCHAR(25) DEFAULT 'default_name' NOT NULL,
updated_at timestamp,
created_at timestamp,
Expand Down
1 change: 1 addition & 0 deletions test/sql/pgsql.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CREATE TABLE authors(
author_id SERIAL PRIMARY KEY,
parent_author_id INT,
publisher_id INT,
name VARCHAR(25) NOT NULL DEFAULT 'default_name',
updated_at timestamp,
created_at timestamp,
Expand Down
1 change: 1 addition & 0 deletions test/sql/sqlite.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CREATE TABLE authors(
author_id INTEGER NOT NULL PRIMARY KEY,
parent_author_id INT,
publisher_id INT,
name VARCHAR (25) NOT NULL DEFAULT default_name, -- don't touch those spaces
updated_at datetime,
created_at datetime,
Expand Down

0 comments on commit e6d2dad

Please sign in to comment.