Skip to content
Permalink
Browse files

No updating the primary key on save

  • Loading branch information...
lorenzo committed Oct 27, 2013
1 parent 12e330e commit 467f08eb5d2f12db28eb27c44dd13d83cf7456ef
Showing with 42 additions and 0 deletions.
  1. +1 −0 Cake/ORM/Table.php
  2. +41 −0 Cake/Test/TestCase/ORM/TableTest.php
@@ -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)
@@ -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.
You can’t perform that action at this time.