Browse Source

Adding joinData to the target relation array wehn using matching

The scenario that c41f40cd was wondering about came to be sooner than
expected.
Jose Lorenzo Rodriguez 12 years ago
parent
commit
9c2c00e21f
3 changed files with 13 additions and 7 deletions
  1. 6 2
      src/ORM/Association/BelongsToMany.php
  2. 1 1
      src/ORM/ResultSet.php
  3. 6 4
      tests/TestCase/ORM/QueryTest.php

+ 6 - 2
src/ORM/Association/BelongsToMany.php

@@ -249,9 +249,13 @@ class BelongsToMany extends Association {
  * @param array $row
  * @return array
  */
-	public function transformRow($row) {
+	public function transformRow($row, $joined = false) {
+		$alias = $this->junction()->alias();
+		if ($joined) {
+			$row[$this->target()->alias()]['_joinData'] = $row[$alias];
+			unset($row[$alias]);
+		}
 		$row = $this->_transformRow($row);
-		unset($row[$this->junction()->alias()]);
 		return $row;
 	}
 

+ 1 - 1
src/ORM/ResultSet.php

@@ -404,7 +404,7 @@ class ResultSet implements Countable, Iterator, Serializable, JsonSerializable {
 				$results[$alias] = $entity;
 			}
 
-			$results = $instance->transformRow($results);
+			$results = $instance->transformRow($results, $assoc['canBeJoined']);
 		}
 
 		foreach ($presentAliases as $alias => $present) {

+ 6 - 4
tests/TestCase/ORM/QueryTest.php

@@ -659,7 +659,8 @@ class QueryTest extends TestCase {
 				'published' => 'Y',
 				'tags' => [
 					'id' => 3,
-					'name' => 'tag3'
+					'name' => 'tag3',
+					'_joinData' => ['article_id' => 2, 'tag_id' => 3]
 				]
 			]
 		];
@@ -681,7 +682,8 @@ class QueryTest extends TestCase {
 				'published' => 'Y',
 				'tags' => [
 					'id' => 2,
-					'name' => 'tag2'
+					'name' => 'tag2',
+					'_joinData' => ['article_id' => 1, 'tag_id' => 2]
 				]
 			]
 		];
@@ -719,7 +721,8 @@ class QueryTest extends TestCase {
 					'published' => 'Y',
 					'tags' => [
 						'id' => 2,
-						'name' => 'tag2'
+						'name' => 'tag2',
+						'_joinData' => ['article_id' => 1, 'tag_id' => 2]
 					]
 				]
 			]
@@ -1873,7 +1876,6 @@ class QueryTest extends TestCase {
 		$table->belongsTo('Tags');
 		TableRegistry::get('Tags')->belongsToMany('Articles');
 		$results = $table->find()->contain(['Articles', 'Tags.Articles'])->hydrate(false)->toArray();
-		debug($results);
 	}
 
 }