Skip to content

Commit

Permalink
Taking in considerations the persisted status of the entity for deciding
Browse files Browse the repository at this point in the history
whether to insert or update
  • Loading branch information
lorenzo committed Oct 27, 2013
1 parent 72e9eeb commit 12e330e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Cake/ORM/Table.php
Expand Up @@ -738,7 +738,9 @@ protected function _processSave($entity, $options) {
$primary = $entity->extract((array)$this->primaryKey());
if ($primary && $entity->isNew() === null) {
$entity->isNew(!$this->exists($primary));
} else {
}

if ($entity->isNew() === null) {
$entity->isNew(true);
}

Expand Down
24 changes: 23 additions & 1 deletion Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -1361,7 +1361,29 @@ public function testSaveUpdateAuto() {
$this->assertEquals($original->password, $row->password);
$this->assertEquals($original->created, $row->created);
$this->assertEquals($original->updated, $row->updated);
$this->assertFalse($entity->isNew());
$this->assertFalse($entity->dirty('id'));
$this->assertFalse($entity->dirty('username'));
}


/**
* Tests that marking an entity as already persisted will prevent the save
* method from trying to infer the entity's actual status.
*
* @return void
*/
public function testSaveUpdateWithHint() {
$table = $this->getMock(
'\Cake\ORM\Table',
['exists'],
[['table' => 'users', 'connection' => ConnectionManager::get('test')]]
);
$entity = new \Cake\ORM\Entity([
'id' => 2,
'username' => 'baggins'
], ['markNew' => false]);
$this->assertFalse($entity->isNew());
$table->expects($this->never())->method('exists');
$this->assertSame($entity, $table->save($entity));
}
}

0 comments on commit 12e330e

Please sign in to comment.