Skip to content

Commit

Permalink
Adding test for proving that aplying formatters to deep associations …
Browse files Browse the repository at this point in the history
…using extra queries is possible
  • Loading branch information
lorenzo committed Feb 9, 2014
1 parent ec0cc6a commit 9937703
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/ORM/EagerLoader.php
Expand Up @@ -198,7 +198,9 @@ protected function _reformatContain($associations, $original) {
}

if (is_array($options)) {
$options = isset($options['config']) ? $options['config'] : $options;
$options = isset($options['config']) ?
$options['config'] + $options['associations'] :
$options;
$options = $this->_reformatContain($options, []);
}

Expand Down
34 changes: 34 additions & 0 deletions tests/TestCase/ORM/QueryTest.php
Expand Up @@ -1656,4 +1656,38 @@ public function testFormatDeepAssocationRecords() {
$expected = ['1 - 3 - 3', '2 - 3 - 3', '1 - 4 - 5', '3 - 4 - 5'];
$this->assertEquals($expected, $query->toArray());
}

/**
* Tests that formatters cna be applied to deep associaitons that are fetched using
* additional queries
*
* @return void
*/
public function testFormatDeepDistantAssociationRecords() {
$table = TableRegistry::get('authors');
$table->hasMany('articles');
$articles = $table->association('articles')->target();
$articles->hasMany('articlesTags');
$articles->association('articlesTags')->target()->belongsTo('tags');

$query = $table->find()->contain(['articles.articlesTags.tags' => function($q) {
return $q->formatResults(function($results) {
return $results->map(function($tag) {
$tag->name .= ' - visited';
return $tag;
});
});
}]);

$query->mapReduce(function($row, $key, $mr) {
foreach ((array)$row->articles as $article) {
foreach ((array)$article->articles_tags as $articleTag) {
$mr->emit($articleTag->tag->name);
}
}
});
$expected = ['tag1 - visited', 'tag2 - visited', 'tag1 - visited', 'tag3 - visited'];
$this->assertEquals($expected, $query->toArray());
}

}

0 comments on commit 9937703

Please sign in to comment.