Permalink
Browse files

patched Entity->modified() to detect changed and unchanged fields

  • Loading branch information...
1 parent 95e03fe commit 945859b284f1a437262a46b3ba4c1cc316d10dc6 @jmglsn jmglsn committed Apr 12, 2012
Showing with 40 additions and 2 deletions.
  1. +11 −2 data/Entity.php
  2. +29 −0 tests/cases/data/entity/DocumentTest.php
View
@@ -365,10 +365,19 @@ public function decrement($field, $value = 1) {
* Gets the array of fields modified on this entity.
*
* @return array Returns an array where the keys are entity field names, and the values are
- * always `true`.
+ * `true` for changed fields.
*/
public function modified() {
- return array_fill_keys(array_keys($this->_updated), true);
+ $fields = array_fill_keys(array_keys($this->_updated), false);
+ foreach ($this->_updated as $field => $value) {
+ if (is_object($value) && method_exists($value, 'modified')) {
+ $modified = $value->modified();
+ $fields[$field] = $modified === true || is_array($modified) && in_array(true, $modified, true);
+ } else {
+ $fields[$field] = !isset($this->_data[$field]) || $this->_data[$field] !== $this->_updated[$field];
+ }
+ }
+ return $fields;
}
public function export() {
@@ -114,6 +114,35 @@ public function testSetField() {
$this->assertEqual($expected, $result);
}
+
+ public function testSyncModified() {
+ $doc = new Document();
+ $doc->id = 4;
+ $doc->name = 'Four';
+ $doc->content = 'Lorem ipsum four';
+
+ $expected = array(
+ 'id' => true,
+ 'name' => true,
+ 'content' => true,
+ );
+
+ $this->assertEqual($expected, $doc->modified());
+ $doc->sync();
+
+ $this->assertEqual(array_fill_keys(array_keys($expected), false), $doc->modified());
+
+
+ $doc->id = 5;
+ $expected = array(
+ 'id' => true,
+ 'name' => false,
+ 'content' => false,
+ );
+
+ $this->assertEqual($expected, $doc->modified());
+ }
+
public function testNestedKeyGetSet() {
$doc = new Document(array('model' => $this->_model, 'data' => array(
'name' => 'Bob', 'location' => 'New York, NY', 'profile' => array(

0 comments on commit 945859b

Please sign in to comment.