Browse Source

Merge pull request #4512 from cakephp/3.0-fix-contain-count

3.0 fix contain count
Mark Story 11 years ago
parent
commit
994ceed8c9

+ 1 - 0
src/ORM/Association/SelectableAssociationTrait.php

@@ -160,6 +160,7 @@ trait SelectableAssociationTrait {
  */
 	protected function _buildSubquery($query) {
 		$filterQuery = $query->cleanCopy();
+		$filterQuery->contain([], true);
 
 		$joins = $filterQuery->join();
 		foreach ($joins as $i => $join) {

+ 0 - 1
src/ORM/Query.php

@@ -492,7 +492,6 @@ class Query extends DatabaseQuery implements JsonSerializable {
 		$query->offset(null);
 		$query->mapReduce(null, null, true);
 		$query->formatResults(null, true);
-		$query->contain([], true);
 		return $query;
 	}
 

+ 20 - 0
tests/TestCase/ORM/QueryRegressionTest.php

@@ -439,4 +439,24 @@ class QueryRegressionTest extends TestCase {
 			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);
+	}
+
 }