Skip to content

Commit

Permalink
Fix exists() being called on every save() operation.
Browse files Browse the repository at this point in the history
If an entity isNew and contains no primary key data we should not call
exists() as there is no way for it to exist yet. Exists should continue
to be called when the entity has a primary key value and is marked as
new though as it may or may not exist in the database and we need to
know.

Fixes #4260
  • Loading branch information
markstory committed Aug 15, 2014
1 parent 928adf7 commit 6e9bb5a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ORM/Table.php
Expand Up @@ -1148,7 +1148,7 @@ protected function _processSave($entity, $options) {
$primaryColumns = (array)$this->primaryKey();
$primary = $entity->extract($primaryColumns);

if ($primary && $entity->isNew()) {
if (array_filter($primary) !== [] && $entity->isNew()) {
$alias = $this->alias();
$conditions = [];
foreach ($primary as $k => $v) {
Expand Down
24 changes: 24 additions & 0 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -1212,6 +1212,30 @@ public function testSaveNewEmptyEntity() {
$this->assertFalse($table->save($entity));
}

/**
* Test that saving a new empty entity does not call exists.
*
* @group save
* @return void
*/
public function testSaveNewEntityNoExists() {
$table = $this->getMock(
'Cake\ORM\Table',
['exists'],
[[
'connection' => $this->connection,
'alias' => 'Users',
'table' => 'users',
]]
);
$entity = $table->newEntity(['username' => 'mark']);
$this->assertTrue($entity->isNew());

$table->expects($this->never())
->method('exists');
$this->assertSame($entity, $table->save($entity));
}

/**
* Tests that saving an entity will filter out properties that
* are not present in the table schema when saving
Expand Down

0 comments on commit 6e9bb5a

Please sign in to comment.