Skip to content

Commit

Permalink
No updating the primary key on save
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Oct 27, 2013
1 parent 12e330e commit 467f08e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cake/ORM/Table.php
Expand Up @@ -779,6 +779,7 @@ protected function _insert($entity, $data) {
protected function _update($entity, $data) {
$query = $this->_buildQuery();
$primaryKey = $entity->extract((array)$this->primaryKey());
$data = array_diff_key($data, $primaryKey);
$statement = $query->update($this->table())
->set($data)
->where($primaryKey)
Expand Down
41 changes: 41 additions & 0 deletions Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -1386,4 +1386,45 @@ public function testSaveUpdateWithHint() {
$table->expects($this->never())->method('exists');
$this->assertSame($entity, $table->save($entity));
}

/**
* Tests that when updating the primary key is not passed to the list of
* attributes to change
*
* @return void
*/
public function testSaveUpdatePrimaryKeyNotModified() {
$table = $this->getMock(
'\Cake\ORM\Table',
['_buildQuery'],
[['table' => 'users', 'connection' => ConnectionManager::get('test')]]
);

$query = $this->getMock(
'\Cake\ORM\Query',
['executeStatement', 'addDefaultTypes', 'set'],
[null, $table]
);

$table->expects($this->once())->method('_buildQuery')
->will($this->returnValue($query));

$statement = $this->getMock('\Cake\Database\Statement\StatementDecorator');
$statement->expects($this->once())->method('rowCount')
->will($this->returnValue(1));

$query->expects($this->once())->method('executeStatement')
->will($this->returnValue($statement));

$query->expects($this->once())->method('set')
->with(['username' => 'baggins'])
->will($this->returnValue($query));

$entity = new \Cake\ORM\Entity([
'id' => 2,
'username' => 'baggins'
], ['markNew' => false]);
$this->assertSame($entity, $table->save($entity));
}

}

0 comments on commit 467f08e

Please sign in to comment.