浏览代码

Don't overwrite an entity's id when it was specified.

When an entity is persisted with a specific ID, we should not overwrite
that value with the generated, but unused value.

Refs #7225
Mark Story 10 年之前
父节点
当前提交
0033fefef7
共有 2 个文件被更改,包括 5 次插入0 次删除
  1. 4 0
      src/ORM/Table.php
  2. 1 0
      tests/TestCase/ORM/TableUuidTest.php

+ 4 - 0
src/ORM/Table.php

@@ -1487,7 +1487,11 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
         }
         $keys = array_fill(0, count($primary), null);
         $id = (array)$this->_newId($primary) + $keys;
+
+        // Generate primary keys preferring values in $data.
         $primary = array_combine($primary, $id);
+        $primary = array_intersect_key($data, $primary) + $primary;
+
         $filteredKeys = array_filter($primary, 'strlen');
         $data = $data + $filteredKeys;
 

+ 1 - 0
tests/TestCase/ORM/TableUuidTest.php

@@ -97,6 +97,7 @@ class TableUuidTest extends TestCase
         ]);
         $table = TableRegistry::get('uuiditems');
         $this->assertSame($entity, $table->save($entity));
+        $this->assertSame($id, $entity->id);
 
         $row = $table->find('all')->where(['id' => $id])->first();
         $this->assertNotEmpty($row);