Skip to content

Commit

Permalink
Allow formatting join tables
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Oct 29, 2016
1 parent 02f5af9 commit 341fb32
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/ORM/Association/BelongsToMany.php
Expand Up @@ -1285,7 +1285,11 @@ protected function _buildQuery($options)
$query
->eagerLoader()
->addToJoinsMap($tempName, $assoc, false, $this->_junctionProperty);
$assoc->attachTo($query, ['aliasPath' => $assoc->alias(), 'includeFields' => false]);
$assoc->attachTo($query, [
'aliasPath' => $assoc->alias(),
'includeFields' => false,
'propertyPath' => $this->_junctionProperty
]);

return $query;
}
Expand Down
19 changes: 14 additions & 5 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -1312,7 +1312,10 @@ public function testFormatResultsBelongsToMany()
->eventManager()
->attach(function ($event, $query) {
$query->formatResults(function ($results) {
$results->beforeFind = true;
foreach ($results as $result) {
$result->beforeFind = true;
}

return $results;
});
}, 'Model.beforeFind');
Expand All @@ -1333,21 +1336,27 @@ public function testFormatResultsBelongsToMany()
$expected = [
'id' => 1,
'name' => 'tag1',
'_joinData' => ['article_id' => 1, 'tag_id' => 1],
'_joinData' => [
'article_id' => 1,
'tag_id' => 1,
'beforeFind' => true,
],
'description' => 'A big description',
'created' => new Time('2016-01-01 00:00'),
'beforeFind' => true,
];
$this->assertEquals($expected, $first->tags[0]->toArray());
$this->assertInstanceOf(Time::class, $first->tags[0]->created);

$expected = [
'id' => 2,
'name' => 'tag2',
'_joinData' => ['article_id' => 1, 'tag_id' => 2],
'_joinData' => [
'article_id' => 1,
'tag_id' => 2,
'beforeFind' => true,
],
'description' => 'Another big description',
'created' => new Time('2016-01-01 00:00'),
'beforeFind' => true,
];
$this->assertEquals($expected, $first->tags[1]->toArray());
$this->assertInstanceOf(Time::class, $first->tags[0]->created);
Expand Down

0 comments on commit 341fb32

Please sign in to comment.