Browse Source

Merge pull request #6255 from rchavik/hasmany-ids-fields

Backward compatibility for #6254
José Lorenzo Rodríguez 11 years ago
parent
commit
639fcc4aae
3 changed files with 61 additions and 3 deletions
  1. 19 3
      src/ORM/Marshaller.php
  2. 24 0
      tests/TestCase/ORM/MarshallerTest.php
  3. 18 0
      tests/TestCase/ORM/TableTest.php

+ 19 - 3
src/ORM/Marshaller.php

@@ -223,6 +223,9 @@ class Marshaller
         if ($assoc->type() === Association::MANY_TO_MANY) {
             return $marshaller->_belongsToMany($assoc, $value, (array)$options);
         }
+        if ($assoc->type() === Association::ONE_TO_MANY && array_key_exists('_ids', $value) && is_array($value['_ids'])) {
+            return $this->_loadAssociatedByIds($assoc, $value['_ids']);
+        }
         return $marshaller->many($value, (array)$options);
     }
 
@@ -266,7 +269,7 @@ class Marshaller
         $associated = isset($options['associated']) ? $options['associated'] : [];
         $hasIds = array_key_exists('_ids', $data);
         if ($hasIds && is_array($data['_ids'])) {
-            return $this->_loadBelongsToMany($assoc, $data['_ids']);
+            return $this->_loadAssociatedByIds($assoc, $data['_ids']);
         }
         if ($hasIds) {
             return [];
@@ -313,7 +316,7 @@ class Marshaller
      * @param array $ids The list of ids to load.
      * @return array An array of entities.
      */
-    protected function _loadBelongsToMany($assoc, $ids)
+    protected function _loadAssociatedByIds($assoc, $ids)
     {
         $target = $assoc->target();
         $primaryKey = (array)$target->primaryKey();
@@ -335,6 +338,19 @@ class Marshaller
     }
 
     /**
+     * Loads a list of belongs to many from ids.
+     *
+     * @param Association $assoc The association class for the belongsToMany association.
+     * @param array $ids The list of ids to load.
+     * @return array An array of entities.
+     * @deprecated Use _loadAssociatedByIds()
+     */
+    protected function _loadBelongsToMany($assoc, $ids)
+    {
+        return $this->_loadAssociatedByIds($assoc, $ids);
+    }
+
+    /**
      * Merges `$data` into `$entity` and recursively does the same for each one of
      * the association names passed in `$options`. When merging associations, if an
      * entity is not present in the parent entity for a given association, a new one
@@ -557,7 +573,7 @@ class Marshaller
         $hasIds = array_key_exists('_ids', $value);
         $associated = isset($options['associated']) ? $options['associated'] : [];
         if ($hasIds && is_array($value['_ids'])) {
-            return $this->_loadBelongsToMany($assoc, $value['_ids']);
+            return $this->_loadAssociatedByIds($assoc, $value['_ids']);
         }
         if ($hasIds) {
             return [];

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

@@ -558,6 +558,30 @@ class MarshallerTest extends TestCase
     }
 
     /**
+     * Test HasMany association with _ids attribute
+     *
+     * @return void
+     */
+    public function testHasManyWithIds()
+    {
+        $data = [
+            'username' => 'lux',
+            'password' => 'passphrase',
+            'comments' => [
+                '_ids' => [1, 2]
+            ]
+        ];
+
+        $userTable = TableRegistry::get('Users');
+        $userTable->hasMany('Comments');
+        $commentTable = TableRegistry::get('Comments');
+        $user = $userTable->newEntity($data, ['associated' => ['Comments']]);
+
+        $this->assertEquals($user->comments[0], $commentTable->get(1));
+        $this->assertEquals($user->comments[1], $commentTable->get(2));
+    }
+
+    /**
      * Test one() with deeper associations.
      *
      * @return void

+ 18 - 0
tests/TestCase/ORM/TableTest.php

@@ -4058,6 +4058,24 @@ class TableTest extends TestCase
         $this->assertEquals(4, $cloned->id);
     }
 
+    public function testSaveHasManyWithIds()
+    {
+        $data = [
+            'username' => 'lux',
+            'password' => 'passphrase',
+            'comments' => [
+                '_ids' => [1, 2]
+            ]
+        ];
+
+        $userTable = TableRegistry::get('Users');
+        $userTable->hasMany('Comments');
+        $savedUser = $userTable->save($userTable->newEntity($data, ['associated' => ['Comments']]));
+        $retrievedUser = $userTable->find('all')->where(['id' => $savedUser->id])->contain(['Comments'])->first();
+        $this->assertEquals($savedUser->comments[0]->user_id, $retrievedUser->comments[0]->user_id);
+        $this->assertEquals($savedUser->comments[1]->user_id, $retrievedUser->comments[1]->user_id);
+    }
+
     /**
      * Tests that after saving then entity contains the right primary
      * key casted to the right type