Browse Source

Mark fields as not dirty when they are unset.

Leaving unset fields as dirty makes entity create null values when
persisted.

Refs #4288
mark_story 11 years ago
parent
commit
63e6d380bf
2 changed files with 13 additions and 0 deletions
  1. 1 0
      src/Datasource/EntityTrait.php
  2. 12 0
      tests/TestCase/ORM/EntityTest.php

+ 1 - 0
src/Datasource/EntityTrait.php

@@ -308,6 +308,7 @@ trait EntityTrait {
 		$property = (array)$property;
 		foreach ($property as $p) {
 			unset($this->_properties[$p]);
+			unset($this->_dirty[$p]);
 		}
 
 		return $this;

+ 12 - 0
tests/TestCase/ORM/EntityTest.php

@@ -288,6 +288,18 @@ class EntityTest extends TestCase {
 	}
 
 /**
+ * Unsetting a property should not mark it as dirty.
+ *
+ * @return void
+ */
+	public function testUnsetMakesClean() {
+		$entity = new Entity(['id' => 1, 'name' => 'bar']);
+		$this->assertTrue($entity->dirty('name'));
+		$entity->unsetProperty('name');
+		$this->assertFalse($entity->dirty('name'), 'Removed properties are not dirty.');
+	}
+
+/**
  * Tests unsetProperty whith multiple properties
  *
  * @return void