Permalink
Browse files

Adding test for proving that aplying formatters to deep associations …

…using extra queries is possible
  • Loading branch information...
1 parent ec0cc6a commit 9937703807c2d3b7baf06e88b835419d2cbb086c @lorenzo lorenzo committed Feb 7, 2014
Showing with 37 additions and 1 deletion.
  1. +3 −1 src/ORM/EagerLoader.php
  2. +34 −0 tests/TestCase/ORM/QueryTest.php
View
4 src/ORM/EagerLoader.php
@@ -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, []);
}
View
34 tests/TestCase/ORM/QueryTest.php
@@ -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.