Browse Source

Placing data generated by matching into a _matchingData key for each row.

As the data completely breaks how entities are naturally nested, it makes
more sense to have it in its own place. This also permits using matching
and contain in the same query
Jose Lorenzo Rodriguez 11 years ago
parent
commit
5571df729c
3 changed files with 46 additions and 25 deletions
  1. 3 3
      src/ORM/Association/BelongsToMany.php
  2. 10 0
      src/ORM/ResultSet.php
  3. 33 22
      tests/TestCase/ORM/QueryTest.php

+ 3 - 3
src/ORM/Association/BelongsToMany.php

@@ -233,9 +233,9 @@ class BelongsToMany extends Association {
 		unset($options['queryBuilder']);
 		$options = ['conditions' => [$cond]] + compact('includeFields');
 		$options['foreignKey'] = $this->targetForeignKey();
-		$this->_targetTable
-			->association($junction->alias())
-			->attachTo($query, $options);
+		$assoc = $this->_targetTable->association($junction->alias());
+		$assoc->attachTo($query, $options);
+		$query->eagerLoader()->addToJoinsMap($junction->alias(), $assoc);
 	}
 
 /**

+ 10 - 0
src/ORM/ResultSet.php

@@ -402,6 +402,12 @@ class ResultSet implements ResultSetInterface {
 				$results[$alias] = $entity;
 			}
 
+			if ($assoc['matching']) {
+				$results['_matchingData'][$alias] = $results[$alias];
+				unset($results[$alias]);
+				continue;
+			}
+
 			$results = $instance->transformRow($results, $alias, $assoc['canBeJoined']);
 		}
 
@@ -412,6 +418,10 @@ class ResultSet implements ResultSetInterface {
 			$results[$defaultAlias][$alias] = $results[$alias];
 		}
 
+		if (isset($results['_matchingData'])) {
+			$results[$defaultAlias]['_matchingData'] = $results['_matchingData'];
+		}
+
 		$options['source'] = $defaultAlias;
 		$results = $results[$defaultAlias];
 		if ($this->_hydrate && !($results instanceof Entity)) {

+ 33 - 22
tests/TestCase/ORM/QueryTest.php

@@ -637,12 +637,14 @@ class QueryTest extends TestCase {
 			[
 				'id' => 3,
 				'name' => 'larry',
-				'articles' => [
-					'id' => 2,
-					'title' => 'Second Article',
-					'body' => 'Second Article Body',
-					'author_id' => 3,
-					'published' => 'Y',
+				'_matchingData' => [
+					'articles' => [
+						'id' => 2,
+						'title' => 'Second Article',
+						'body' => 'Second Article Body',
+						'author_id' => 3,
+						'published' => 'Y',
+					]
 				]
 			]
 		];
@@ -678,10 +680,12 @@ class QueryTest extends TestCase {
 				'title' => 'Second Article',
 				'body' => 'Second Article Body',
 				'published' => 'Y',
-				'tags' => [
-					'id' => 3,
-					'name' => 'tag3',
-					'_joinData' => ['article_id' => 2, 'tag_id' => 3]
+				'_matchingData' => [
+					'Tags' => [
+						'id' => 3,
+						'name' => 'tag3',
+						'articles_tags' => ['article_id' => 2, 'tag_id' => 3]
+					]
 				]
 			]
 		];
@@ -701,10 +705,12 @@ class QueryTest extends TestCase {
 				'body' => 'First Article Body',
 				'author_id' => 1,
 				'published' => 'Y',
-				'tags' => [
-					'id' => 2,
-					'name' => 'tag2',
-					'_joinData' => ['article_id' => 1, 'tag_id' => 2]
+				'_matchingData' => [
+					'Tags' => [
+						'id' => 2,
+						'name' => 'tag2',
+						'articles_tags' => ['article_id' => 1, 'tag_id' => 2]
+					]
 				]
 			]
 		];
@@ -734,16 +740,21 @@ class QueryTest extends TestCase {
 			[
 				'id' => 1,
 				'name' => 'mariano',
-				'articles' => [
-					'id' => 1,
-					'title' => 'First Article',
-					'body' => 'First Article Body',
-					'author_id' => 1,
-					'published' => 'Y',
+				'_matchingData' => [
 					'tags' => [
 						'id' => 2,
 						'name' => 'tag2',
-						'_joinData' => ['article_id' => 1, 'tag_id' => 2]
+						'articles_tags' => [
+							'article_id' => 1,
+							'tag_id' => 2
+						]
+					],
+					'articles' => [
+						'id' => 1,
+						'author_id' => 1,
+						'title' => 'First Article',
+						'body' => 'First Article Body',
+						'published' => 'Y'
 					]
 				]
 			]
@@ -1856,7 +1867,7 @@ class QueryTest extends TestCase {
 
 		$results = $query->toArray();
 		$this->assertCount(1, $results);
-		$this->assertEquals('tag3', $results[0]->articles->articles_tags->tag->name);
+		$this->assertEquals('tag3', $results[0]->_matchingData['tags']->name);
 	}
 
 /**