Skip to content

Commit

Permalink
Don't overwrite an entity's id when it was specified.
Browse files Browse the repository at this point in the history
When an entity is persisted with a specific ID, we should not overwrite
that value with the generated, but unused value.

Refs #7225
  • Loading branch information
markstory committed Aug 30, 2015
1 parent bbe5357 commit 0033fef
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/ORM/Table.php
Expand Up @@ -1487,7 +1487,11 @@ protected function _insert($entity, $data)
}
$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;

Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/ORM/TableUuidTest.php
Expand Up @@ -97,6 +97,7 @@ public function testSaveNewSpecificId()
]);
$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);
Expand Down

0 comments on commit 0033fef

Please sign in to comment.