Skip to content

Commit

Permalink
Don't save entities that haven't changed since last time they were
Browse files Browse the repository at this point in the history
persisted
  • Loading branch information
lorenzo committed Nov 29, 2013
1 parent 8fd6998 commit cdd24b9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Cake/ORM/Table.php
Expand Up @@ -923,6 +923,11 @@ public function save(Entity $entity, array $options = []) {
'validate' => true,
'associated' => true
]);

if ($entity->isNew() === false && !$entity->dirty()) {
return $entity;
}

if ($options['atomic']) {
$connection = $this->connection();
$success = $connection->transactional(function() use ($entity, $options) {
Expand Down
2 changes: 1 addition & 1 deletion Cake/Test/TestCase/ORM/EntityTest.php
Expand Up @@ -475,7 +475,7 @@ public function testDirty() {
$entity->dirty('author_id', false);
$this->assertFalse($entity->dirty());
}

/**
* Tests dirty() when altering properties values and adding new ones
*
Expand Down
16 changes: 16 additions & 0 deletions Cake/Test/TestCase/ORM/TableTest.php
Expand Up @@ -2418,4 +2418,20 @@ public function testSaveBelongsToWithValidationErrorInJointEntityNonAtomic() {
$this->assertEquals(5, $entity->tags[1]->extraInfo->tag_id);
}

/**
* Tests that saving a persisted and clean entity will is a no-op
*
* @group save
* @return void
*/
public function testSaveCleanEntity() {
$table = $this->getMock('\Cake\ORM\Table', ['_processSave']);
$entity = new \Cake\ORM\Entity(
['id' => 'foo'],
['markNew' => false, 'markClean' => true]
);
$table->expects($this->never())->method('_processSave');
$this->assertSame($entity, $table->save($entity));
}

}

0 comments on commit cdd24b9

Please sign in to comment.