Browse Source

Consistently read the PK from the table (instead of the schema).

ndm2 9 years ago
parent
commit
df3479a426
2 changed files with 56 additions and 2 deletions
  1. 2 2
      src/ORM/Marshaller.php
  2. 54 0
      tests/TestCase/ORM/MarshallerTest.php

+ 2 - 2
src/ORM/Marshaller.php

@@ -119,7 +119,7 @@ class Marshaller
         $propertyMap = $this->_buildPropertyMap($options);
 
         $schema = $this->_table->schema();
-        $primaryKey = $schema->primaryKey();
+        $primaryKey = (array)$this->_table->primaryKey();
         $entityClass = $this->_table->entityClass();
         $entity = new $entityClass();
         $entity->source($this->_table->registryAlias());
@@ -314,7 +314,7 @@ class Marshaller
         $data = array_values($data);
 
         $target = $assoc->target();
-        $primaryKey = array_flip($target->schema()->primaryKey());
+        $primaryKey = array_flip((array)$target->primaryKey());
         $records = $conditions = [];
         $primaryCount = count($primaryKey);
         $conditions = [];

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

@@ -2923,4 +2923,58 @@ class MarshallerTest extends TestCase
         $this->assertFalse($entity->dirty('user'));
         $this->assertTrue($entity->user->isNew());
     }
+
+    public function testEnsurePrimaryKeyBeingReadFromTableForHandlingEmptyStringPrimaryKey()
+    {
+        $data = [
+            'id' => ''
+        ];
+
+        $articles = TableRegistry::get('Articles');
+        $articles->schema()->dropConstraint('primary');
+        $articles->primaryKey('id');
+
+        $marshall = new Marshaller($articles);
+        $result = $marshall->one($data);
+
+        $this->assertFalse($result->dirty('id'));
+        $this->assertNull($result->id);
+    }
+
+    public function testEnsurePrimaryKeyBeingReadFromTableWhenLoadingBelongsToManyRecordsByPrimaryKey()
+    {
+        $data = [
+            'tags' => [
+                [
+                    'id' => 1
+                ],
+                [
+                    'id' => 2
+                ]
+            ]
+        ];
+
+        $tags = TableRegistry::get('Tags');
+        $tags->schema()->dropConstraint('primary');
+        $tags->primaryKey('id');
+
+        $marshall = new Marshaller($this->articles);
+        $result = $marshall->one($data, ['associated' => ['Tags']]);
+
+        $expected = [
+            'tags' => [
+                [
+                    'id' => 1,
+                    'name' => 'tag1',
+                    'description' => 'A big description'
+                ],
+                [
+                    'id' => 2,
+                    'name' => 'tag2',
+                    'description' => 'Another big description'
+                ]
+            ]
+        ];
+        $this->assertEquals($expected, $result->toArray());
+    }
 }