Browse Source

Merge pull request #6080 from cakephp/3.0-fix-pk-type

Quick solution for casting result primary keys to the correct type
Mark Story 11 years ago
parent
commit
697809dd1c
2 changed files with 25 additions and 1 deletions
  1. 4 1
      src/ORM/Table.php
  2. 21 0
      tests/TestCase/ORM/TableTest.php

+ 4 - 1
src/ORM/Table.php

@@ -1505,10 +1505,13 @@ class Table implements RepositoryInterface, EventListenerInterface
         if ($statement->rowCount() !== 0) {
             $success = $entity;
             $entity->set($filteredKeys, ['guard' => false]);
+            $schema = $this->schema();
+            $driver = $this->connection()->driver();
             foreach ($primary as $key => $v) {
                 if (!isset($data[$key])) {
                     $id = $statement->lastInsertId($this->table(), $key);
-                    $entity->set($key, $id);
+                    $type = $schema->columnType($key);
+                    $entity->set($key, Type::build($type)->toPHP($id, $driver));
                     break;
                 }
             }

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

@@ -4028,6 +4028,7 @@ class TableTest extends TestCase
      * Tests that passing a coned entity that was marked as new to save() will
      * actaully save it as a new entity
      *
+     * @group save
      * @return void
      */
     public function testSaveWithClonedEntity()
@@ -4047,6 +4048,26 @@ class TableTest extends TestCase
     }
 
     /**
+     * Tests that after saving then entity contains the right primary
+     * key casted to the right type
+     *
+     * @group save
+     * @return void
+     */
+    public function testSaveCorrectPrimaryKeyType()
+    {
+        $entity = new Entity([
+            'username' => 'superuser',
+            'created' => new Time('2013-10-10 00:00'),
+            'updated' => new Time('2013-10-10 00:00')
+        ], ['markNew' => true]);
+
+        $table = TableRegistry::get('Users');
+        $this->assertSame($entity, $table->save($entity));
+        $this->assertSame(self::$nextUserId, $entity->id);
+    }
+
+    /**
      * Helper method to skip tests when connection is SQLServer.
      *
      * @return void