Skip to content

Commit bcf66a2

Browse files
committed
Only saving properties that were marked as dirty
1 parent 67571cf commit bcf66a2

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

Cake/ORM/Table.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,16 @@ protected function _processSave($entity, $options) {
711711

712712
$schema = $this->schema();
713713
$data = array_intersect_key($data, array_flip($schema->columns()));
714+
$keys = array_keys($data);
715+
716+
foreach ($keys as $i => $property) {
717+
if (!$entity->dirty($property)) {
718+
unset($keys[$i], $data[$property]);
719+
}
720+
}
721+
714722
$query = $this->_buildQuery();
715-
$statement = $query->insert($this->table(), array_keys($data))
723+
$statement = $query->insert($this->table(), $keys)
716724
->values($data)
717725
->executeStatement();
718726

@@ -721,6 +729,7 @@ protected function _processSave($entity, $options) {
721729
$primary = $this->primaryKey();
722730
$id = $statement->lastInsertId($this->table(), $primary);
723731
$entity->set($primary, $id);
732+
$entity->clean();
724733
$success = $entity;
725734
}
726735

Cake/Test/TestCase/ORM/TableTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,4 +1238,51 @@ public function testAtomicSaveRollbackOnFailure() {
12381238
$table->save($data);
12391239
}
12401240

1241+
/**
1242+
* Tests that only the properties marked as dirty are actually saved
1243+
* to the database
1244+
*
1245+
* @return void
1246+
*/
1247+
public function testSaveOnlyDirtyProperties() {
1248+
$entity = new \Cake\ORM\Entity([
1249+
'username' => 'superuser',
1250+
'password' => 'root',
1251+
'created' => new \DateTime('2013-10-10 00:00'),
1252+
'updated' => new \DateTime('2013-10-10 00:00')
1253+
]);
1254+
$entity->clean();
1255+
$entity->dirty('username', true);
1256+
$entity->dirty('created', true);
1257+
$entity->dirty('updated', true);
1258+
1259+
$table = TableRegistry::get('users');
1260+
$this->assertSame($entity, $table->save($entity));
1261+
$this->assertEquals($entity->id, 5);
1262+
1263+
$row = $table->find('all')->where(['id' => 5])->first();
1264+
$entity->set('password', null);
1265+
$this->assertEquals($entity->toArray(), $row->toArray());
1266+
}
1267+
1268+
/**
1269+
* Tests that a recently saved entity is marked as clean
1270+
*
1271+
* @return void
1272+
*/
1273+
public function testsASavedEntityIsClean() {
1274+
$entity = new \Cake\ORM\Entity([
1275+
'username' => 'superuser',
1276+
'password' => 'root',
1277+
'created' => new \DateTime('2013-10-10 00:00'),
1278+
'updated' => new \DateTime('2013-10-10 00:00')
1279+
]);
1280+
$table = TableRegistry::get('users');
1281+
$this->assertSame($entity, $table->save($entity));
1282+
$this->assertFalse($entity->dirty('usermane'));
1283+
$this->assertFalse($entity->dirty('password'));
1284+
$this->assertFalse($entity->dirty('created'));
1285+
$this->assertFalse($entity->dirty('updated'));
1286+
}
1287+
12411288
}

0 commit comments

Comments
 (0)