Browse Source

Allow formatting join tables

Jeremy Harris 9 years ago
parent
commit
341fb32bc5
2 changed files with 19 additions and 6 deletions
  1. 5 1
      src/ORM/Association/BelongsToMany.php
  2. 14 5
      tests/TestCase/ORM/QueryTest.php

+ 5 - 1
src/ORM/Association/BelongsToMany.php

@@ -1285,7 +1285,11 @@ class BelongsToMany extends Association
         $query
             ->eagerLoader()
             ->addToJoinsMap($tempName, $assoc, false, $this->_junctionProperty);
-        $assoc->attachTo($query, ['aliasPath' => $assoc->alias(), 'includeFields' => false]);
+        $assoc->attachTo($query, [
+            'aliasPath' => $assoc->alias(),
+            'includeFields' => false,
+            'propertyPath' => $this->_junctionProperty
+        ]);
 
         return $query;
     }

+ 14 - 5
tests/TestCase/ORM/QueryTest.php

@@ -1312,7 +1312,10 @@ class QueryTest extends TestCase
             ->eventManager()
             ->attach(function ($event, $query) {
                 $query->formatResults(function ($results) {
-                    $results->beforeFind = true;
+                    foreach ($results as $result) {
+                        $result->beforeFind = true;
+                    }
+
                     return $results;
                 });
             }, 'Model.beforeFind');
@@ -1333,10 +1336,13 @@ class QueryTest extends TestCase
         $expected = [
             'id' => 1,
             'name' => 'tag1',
-            '_joinData' => ['article_id' => 1, 'tag_id' => 1],
+            '_joinData' => [
+                'article_id' => 1,
+                'tag_id' => 1,
+                'beforeFind' => true,
+            ],
             'description' => 'A big description',
             'created' => new Time('2016-01-01 00:00'),
-            'beforeFind' => true,
         ];
         $this->assertEquals($expected, $first->tags[0]->toArray());
         $this->assertInstanceOf(Time::class, $first->tags[0]->created);
@@ -1344,10 +1350,13 @@ class QueryTest extends TestCase
         $expected = [
             'id' => 2,
             'name' => 'tag2',
-            '_joinData' => ['article_id' => 1, 'tag_id' => 2],
+            '_joinData' => [
+                'article_id' => 1,
+                'tag_id' => 2,
+                'beforeFind' => true,
+            ],
             'description' => 'Another big description',
             'created' => new Time('2016-01-01 00:00'),
-            'beforeFind' => true,
         ];
         $this->assertEquals($expected, $first->tags[1]->toArray());
         $this->assertInstanceOf(Time::class, $first->tags[0]->created);