Skip to content

Commit

Permalink
Adding joinData to the target relation array wehn using matching
Browse files Browse the repository at this point in the history
The scenario that c41f40c was wondering about came to be sooner than
expected.
  • Loading branch information
lorenzo committed Mar 27, 2014
1 parent 6ef9579 commit 9c2c00e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/ORM/Association/BelongsToMany.php
Expand Up @@ -249,9 +249,13 @@ public function attachTo(Query $query, array $options = []) {
* @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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ORM/ResultSet.php
Expand Up @@ -404,7 +404,7 @@ protected function _groupResult($row) {
$results[$alias] = $entity;
}

$results = $instance->transformRow($results);
$results = $instance->transformRow($results, $assoc['canBeJoined']);
}

foreach ($presentAliases as $alias => $present) {
Expand Down
10 changes: 6 additions & 4 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -659,7 +659,8 @@ public function testFilteringByBelongsToManyNoHydration() {
'published' => 'Y',
'tags' => [
'id' => 3,
'name' => 'tag3'
'name' => 'tag3',
'_joinData' => ['article_id' => 2, 'tag_id' => 3]
]
]
];
Expand All @@ -681,7 +682,8 @@ public function testFilteringByBelongsToManyNoHydration() {
'published' => 'Y',
'tags' => [
'id' => 2,
'name' => 'tag2'
'name' => 'tag2',
'_joinData' => ['article_id' => 1, 'tag_id' => 2]
]
]
];
Expand Down Expand Up @@ -719,7 +721,8 @@ public function testMatchingDotNotation() {
'published' => 'Y',
'tags' => [
'id' => 2,
'name' => 'tag2'
'name' => 'tag2',
'_joinData' => ['article_id' => 1, 'tag_id' => 2]
]
]
]
Expand Down Expand Up @@ -1873,7 +1876,6 @@ public function testRepeatedAssociationAliases() {
$table->belongsTo('Tags');
TableRegistry::get('Tags')->belongsToMany('Articles');
$results = $table->find()->contain(['Articles', 'Tags.Articles'])->hydrate(false)->toArray();
debug($results);
}

}

0 comments on commit 9c2c00e

Please sign in to comment.