Skip to content
Permalink
Browse files

Reset eager loaders before triggering before find.

In #10657 I made a change to reset eager loaders when creating a clone
to fix state being shared across query clones. Because of method
ordering this resulted in the eager loader being reset *after* the
beforeFind was triggered, which dropped any contained associations added
during that event. Re-ordering the method calls allows contained
associations added during the beforeFind to be retained in the count()
query.

Refs #10813
  • Loading branch information...
markstory committed Jul 6, 2017
1 parent 02c51aa commit 2d59a95bfef4195d7170288cdfcbc00b1a1c1095
Showing with 29 additions and 1 deletion.
  1. +1 −1 src/ORM/Query.php
  2. +28 −0 tests/TestCase/ORM/QueryRegressionTest.php
@@ -756,6 +756,7 @@ public function applyOptions(array $options)
public function cleanCopy()
{
$clone = clone $this;
$clone->setEagerLoader(clone $this->getEagerLoader());
$clone->triggerBeforeFind();
$clone->enableAutoFields(false);
$clone->limit(null);
@@ -765,7 +766,6 @@ public function cleanCopy()
$clone->formatResults(null, true);
$clone->setSelectTypeMap(new TypeMap());
$clone->decorateResults(null, true);
$clone->setEagerLoader(clone $this->getEagerLoader());
return $clone;
}
@@ -741,6 +741,34 @@ public function testCountWithBind()
$this->assertEquals(1, $count);
}
/**
* Test count() with inner join containments.
*
* @return void
*/
public function testCountWithInnerJoinContain()
{
$this->loadFixtures('Articles', 'Authors');
$table = TableRegistry::get('Articles');
$table->belongsTo('Authors')->setJoinType('INNER');
$result = $table->save($table->newEntity([
'author_id' => null,
'title' => 'title',
'body' => 'body',
'published' => 'Y'
]));
$this->assertNotFalse($result);
$table->eventManager()
->on('Model.beforeFind', function (Event $event, $query) {
$query->contain(['Authors']);
});
$count = $table->find()->count();
$this->assertEquals(3, $count);
}
/**
* Tests that bind in subqueries works.
*

0 comments on commit 2d59a95

Please sign in to comment.
You can’t perform that action at this time.