Browse Source

Fixing issue in contain/mathching regarding dot notation

Previously it was impossible to use dot notation on two
different calls to matching/contain when part of the string was
shared (for example Articles.SpecialTags.Tags and Articles.SpecialTags.Authors)

The issue was that the second call would override the settings
created for the first one.
Jose Lorenzo Rodriguez 10 years ago
parent
commit
60db9e0589

+ 4 - 1
src/ORM/EagerLoader.php

@@ -281,7 +281,10 @@ class EagerLoader
                 $options = isset($options['config']) ?
                     $options['config'] + $options['associations'] :
                     $options;
-                $options = $this->_reformatContain($options, []);
+                $options = $this->_reformatContain(
+                    $options,
+                    isset($pointer[$table]) ? $pointer[$table] : []
+                );
             }
 
             if ($options instanceof Closure) {

+ 2 - 2
tests/Fixture/SpecialTagsFixture.php

@@ -47,7 +47,7 @@ class SpecialTagsFixture extends TestFixture
      * @var array
      */
     public $records = [
-        ['article_id' => 1, 'tag_id' => 3, 'highlighted' => false, 'highlighted_time' => null, 'author_id' => null],
-        ['article_id' => 2, 'tag_id' => 1, 'highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00', 'author_id' => null]
+        ['article_id' => 1, 'tag_id' => 3, 'highlighted' => false, 'highlighted_time' => null, 'author_id' => 1],
+        ['article_id' => 2, 'tag_id' => 1, 'highlighted' => true, 'highlighted_time' => '2014-06-01 10:10:00', 'author_id' => 2]
     ];
 }

+ 29 - 0
tests/TestCase/ORM/QueryRegressionTest.php

@@ -968,4 +968,33 @@ class QueryRegressionTest extends TestCase
         $this->assertNotEmpty($result->tags, 'Missing tags');
         $this->assertNotEmpty($result->tags[0]->_joinData, 'Missing join data');
     }
+
+    /**
+     * Tests that it is possible to use matching with dot notation
+     * even when part of the part of the path in the dot notation is
+     * shared for two different calls
+     *
+     * @return void
+     */
+    public function testDotNotationNotOverride()
+    {
+        $table = TableRegistry::get('Comments');
+        $articles = $table->belongsTo('Articles');
+        $specialTags = $articles->hasMany('SpecialTags');
+        $specialTags->belongsTo('Authors');
+        $specialTags->belongsTo('Tags');
+
+        $results = $table
+            ->find()
+            ->select(['name' => 'Authors.name', 'tag' => 'Tags.name'])
+            ->matching('Articles.SpecialTags.Tags')
+            ->matching('Articles.SpecialTags.Authors', function ($q) {
+                return $q->where(['Authors.id' => 2]);
+            })
+            ->distinct()
+            ->hydrate(false)
+            ->toArray();
+
+        $this->assertEquals([['name' => 'nate', 'tag' => 'tag1']], $results);
+    }
 }