Browse Source

patch marshaller belongsToMany with mixed _joinData

Cees Vogel 11 years ago
parent
commit
bf887e921e
2 changed files with 57 additions and 3 deletions
  1. 17 3
      src/ORM/Marshaller.php
  2. 40 0
      tests/TestCase/ORM/MarshallerTest.php

+ 17 - 3
src/ORM/Marshaller.php

@@ -283,7 +283,7 @@ class Marshaller
         $primaryKey = array_flip($assoc->target()->schema()->primaryKey());
         $records = [];
 
-        foreach ($data as $row) {
+        foreach ($data as $i => $row) {
             if (array_intersect_key($primaryKey, $row) === $primaryKey) {
                 if (!isset($query)) {
                     $primaryCount = count($primaryKey);
@@ -294,12 +294,26 @@ class Marshaller
                     $query->orWhere($keys);
                 }
             } else {
-                $records[] = $this->one($row, $options);
+                $records[$i] = $this->one($row, $options);
             }
         }
 
         if (isset($query)) {
-            $records = array_merge($records, $query->toArray());
+            $queryData = $query->toArray();
+            foreach ($queryData as $queryItem) {
+                foreach ($data as $i => $row) {
+                    $matchCount = 0;
+                    foreach ($primaryKey as $key => $value) {
+                        if (isset($row[$key]) && $row[$key] == $queryItem->$key) {
+                            $matchCount++;
+                        }
+                        if ($matchCount === count($primaryKey)) {
+                            $records[$i] = $queryItem;
+                            break 2;
+                        }
+                    }
+                }
+            }
         }
 
         $joint = $assoc->junction();

+ 40 - 0
tests/TestCase/ORM/MarshallerTest.php

@@ -590,6 +590,46 @@ class MarshallerTest extends TestCase
     }
 
     /**
+     * Test belongsToMany association with mixed data and _joinData
+     *
+     * @return void
+     */
+    public function testBelongsToManyWithMixedJoinData()
+    {
+        $data = [
+            'title' => 'My title',
+            'body' => 'My content',
+            'author_id' => 1,
+            'tags' => [
+                [
+                    'id' => 1,
+                    '_joinData' => [
+                        'active' => 0,
+                    ]
+                ],
+                [
+                    'name' => 'tag5',
+                    '_joinData' => [
+                        'active' => 1,
+                    ]
+                ]
+            ]
+        ];
+
+        $articlesTags = TableRegistry::get('ArticlesTags');
+        $articlesTags->belongsTo('Users');
+
+        $marshall = new Marshaller($this->articles);
+
+        $result = $marshall->one($data, ['associated' => ['Tags._joinData.Users']]);
+
+        $this->assertEquals($data['tags'][0]['id'], $result->tags[0]->id);
+        $this->assertEquals($data['tags'][1]['name'], $result->tags[1]->name);
+        $this->assertEquals(0, $result->tags[0]->_joinData->active);
+        $this->assertEquals(1, $result->tags[1]->_joinData->active);
+    }
+
+    /**
      * Test belongsToMany association with mixed data array
      *
      * @return void