Skip to content

Commit

Permalink
Merge pull request #4512 from cakephp/3.0-fix-contain-count
Browse files Browse the repository at this point in the history
3.0 fix contain count
  • Loading branch information
markstory committed Sep 6, 2014
2 parents c134428 + be67ce8 commit 994ceed
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/ORM/Association/SelectableAssociationTrait.php
Expand Up @@ -160,6 +160,7 @@ protected abstract function _linkField($options);
*/
protected function _buildSubquery($query) {
$filterQuery = $query->cleanCopy();
$filterQuery->contain([], true);

$joins = $filterQuery->join();
foreach ($joins as $i => $join) {
Expand Down
1 change: 0 additions & 1 deletion src/ORM/Query.php
Expand Up @@ -492,7 +492,6 @@ public function cleanCopy() {
$query->offset(null);
$query->mapReduce(null, null, true);
$query->formatResults(null, true);
$query->contain([], true);
return $query;
}

Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/ORM/QueryRegressionTest.php
Expand Up @@ -439,4 +439,24 @@ public function testDeepBelongsToManySubqueryStrategy() {
collection($result[2]->articles[0]->tags)->extract('name')->toArray()
);
}

/**
* Tests that getting the count of a query having containments return
* the correct results
*
* @see https://github.com/cakephp/cakephp/issues/4511
* @return void
*/
public function testCountWithContain() {
$table = TableRegistry::get('Articles');
$table->belongsTo('Authors', ['joinType' => 'inner']);
$count = $table
->find()
->contain(['Authors' => function($q) {
return $q->where(['Authors.id' => 1]);
}])
->count();
$this->assertEquals(2, $count);
}

}

0 comments on commit 994ceed

Please sign in to comment.