Browse Source

Merge pull request #6311 from cakephp/marshal-error

Skip non-entity entries in BelongsToMany associations
José Lorenzo Rodríguez 11 years ago
parent
commit
9124e0b027

+ 6 - 0
src/ORM/Association/BelongsToMany.php

@@ -804,6 +804,9 @@ class BelongsToMany extends Association
         $primary = (array)$target->primaryKey();
         $jointProperty = $this->_junctionProperty;
         foreach ($targetEntities as $k => $entity) {
+            if (!($entity instanceof EntityInterface)) {
+                continue;
+            }
             $key = array_values($entity->extract($primary));
             foreach ($present as $i => $data) {
                 if ($key === $data && !$entity->get($jointProperty)) {
@@ -873,6 +876,9 @@ class BelongsToMany extends Association
         $missing = [];
 
         foreach ($targetEntities as $entity) {
+            if (!($entity instanceof EntityInterface)) {
+                continue;
+            }
             $joint = $entity->get($jointProperty);
 
             if (!$joint || !($joint instanceof EntityInterface)) {

+ 1 - 1
tests/TestCase/ORM/Association/BelongsToManyTest.php

@@ -912,7 +912,7 @@ class BelongsToManyTest extends TestCase
      *
      * @return void
      */
-    public function testSaveAssociatedOnlyEntities()
+    public function testSaveAssociatedOnlyEntitiesAppend()
     {
         $connection = ConnectionManager::get('test');
         $mock = $this->getMock(

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

@@ -3171,6 +3171,22 @@ class TableTest extends TestCase
     }
 
     /**
+     * Test that belongsToMany ignores non-entity data.
+     *
+     * @return void
+     */
+    public function testSaveBelongsToManyIgnoreNonEntityData()
+    {
+        $articles = TableRegistry::get('Articles');
+        $article = $articles->get(1, ['contain' => ['Tags']]);
+        $article->tags = [
+            '_ids' => [2, 1]
+        ];
+        $result = $articles->save($article);
+        $this->assertSame($result, $article);
+    }
+
+    /**
      * Tests that saving a persisted and clean entity will is a no-op
      *
      * @group save