Skip to content

Commit

Permalink
Restructuring how the junction table results are returned for matching,
Browse files Browse the repository at this point in the history
there is no good reason for nesting them into another alias.
  • Loading branch information
lorenzo committed Dec 15, 2014
1 parent 1e6936f commit 77105cf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/ORM/Association/BelongsToMany.php
Expand Up @@ -235,7 +235,7 @@ public function attachTo(Query $query, array $options = []) {
$options['foreignKey'] = $this->targetForeignKey();
$assoc = $this->_targetTable->association($junction->alias());
$assoc->attachTo($query, $options);
$query->eagerLoader()->addToJoinsMap($junction->alias(), $assoc);
$query->eagerLoader()->addToJoinsMap($junction->alias(), $assoc, true);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/ORM/EagerLoader.php
Expand Up @@ -516,7 +516,7 @@ public function associationsMap($table) {
'canBeJoined' => $meta['canBeJoined'],
'entityClass' => $meta['instance']->target()->entityClass(),
'nestKey' => $meta['canBeJoined'] ? $assoc : $meta['aliasPath'],
'matching' => $matching
'matching' => isset($meta['matching']) ? $meta['matching'] : $matching
];
if ($meta['canBeJoined'] && !empty($meta['associations'])) {
$visitor($meta['associations'], $matching);
Expand All @@ -537,13 +537,16 @@ public function associationsMap($table) {
* @param string $alias The table alias as it appears in the query.
* @param \Cake\ORM\Association $assoc The association object the alias represents.
* will be normalized
* @param bool $asMatching Whether or not this join results should be treated as a
* 'matching' association.
* @return void
*/
public function addToJoinsMap($alias, Association $assoc) {
public function addToJoinsMap($alias, Association $assoc, $asMatching = false) {
$this->_joinsMap[$alias] = [
'aliasPath' => $alias,
'instance' => $assoc,
'canBeJoined' => true,
'matching' => $asMatching,
'associations' => []
];
}
Expand Down
18 changes: 9 additions & 9 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -684,8 +684,8 @@ public function testFilteringByBelongsToManyNoHydration() {
'Tags' => [
'id' => 3,
'name' => 'tag3',
'articles_tags' => ['article_id' => 2, 'tag_id' => 3]
]
],
'ArticlesTags' => ['article_id' => 2, 'tag_id' => 3]
]
]
];
Expand All @@ -709,8 +709,8 @@ public function testFilteringByBelongsToManyNoHydration() {
'Tags' => [
'id' => 2,
'name' => 'tag2',
'articles_tags' => ['article_id' => 1, 'tag_id' => 2]
]
],
'ArticlesTags' => ['article_id' => 1, 'tag_id' => 2]
]
]
];
Expand Down Expand Up @@ -744,18 +744,18 @@ public function testMatchingDotNotation() {
'tags' => [
'id' => 2,
'name' => 'tag2',
'articles_tags' => [
'article_id' => 1,
'tag_id' => 2
]
],
'articles' => [
'id' => 1,
'author_id' => 1,
'title' => 'First Article',
'body' => 'First Article Body',
'published' => 'Y'
]
],
'ArticlesTags' => [
'article_id' => 1,
'tag_id' => 2
]
]
]
];
Expand Down

0 comments on commit 77105cf

Please sign in to comment.