Skip to content

Commit

Permalink
Placing data generated by matching into a _matchingData key for each …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
lorenzo committed Dec 13, 2014
1 parent bc07bcf commit 5571df7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/ORM/Association/BelongsToMany.php
Expand Up @@ -233,9 +233,9 @@ public function attachTo(Query $query, array $options = []) {
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);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/ORM/ResultSet.php
Expand Up @@ -402,6 +402,12 @@ protected function _groupResult($row) {
$results[$alias] = $entity;
}

if ($assoc['matching']) {
$results['_matchingData'][$alias] = $results[$alias];
unset($results[$alias]);
continue;
}

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

Expand All @@ -412,6 +418,10 @@ protected function _groupResult($row) {
$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)) {
Expand Down
55 changes: 33 additions & 22 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -637,12 +637,14 @@ public function testFilteringByHasManyNoHydration() {
[
'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',
]
]
]
];
Expand Down Expand Up @@ -678,10 +680,12 @@ public function testFilteringByBelongsToManyNoHydration() {
'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]
]
]
]
];
Expand All @@ -701,10 +705,12 @@ public function testFilteringByBelongsToManyNoHydration() {
'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]
]
]
]
];
Expand Down Expand Up @@ -734,16 +740,21 @@ public function testMatchingDotNotation() {
[
'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'
]
]
]
Expand Down Expand Up @@ -1856,7 +1867,7 @@ public function testContainInAssociationMatching() {

$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);
}

/**
Expand Down

0 comments on commit 5571df7

Please sign in to comment.