Skip to content

Commit

Permalink
Allow any contain call to overwrite the eagerloader.
Browse files Browse the repository at this point in the history
The override parameter should always work when used.

Refs #7412
  • Loading branch information
markstory committed Sep 20, 2015
1 parent 06c7f1e commit b417193
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ORM/Query.php
Expand Up @@ -287,7 +287,7 @@ public function eagerLoader(EagerLoader $instance = null)
*/
public function contain($associations = null, $override = false)
{
if (empty($associations) && $override) {
if ($override) {
$this->_eagerLoader = null;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -1822,6 +1822,28 @@ public function testCacheIntegrationWithFormatResults()
$this->assertSame($expected, $results);
}

/**
* Test overwriting the contained associations.
*
* @return void
*/
public function testContainOverwrite()
{
$table = TableRegistry::get('Articles');
$table->hasMany('Comments');
$table->belongsTo('Authors');

$query = $table->find();
$query->contain(['Comments']);
$this->assertEquals(['Comments'], array_keys($query->contain()));

$query->contain(['Authors'], true);
$this->assertEquals(['Authors'], array_keys($query->contain()));

$query->contain(['Comments', 'Authors'], true);
$this->assertEquals(['Comments', 'Authors'], array_keys($query->contain()));
}

/**
* Integration test to show filtering associations using contain and a closure
*
Expand Down

0 comments on commit b417193

Please sign in to comment.