Browse Source

Timestampable now works with change tracking policy notify

Sascha-Oliver Prolic 13 years ago
parent
commit
2f84c188a0
1 changed files with 36 additions and 7 deletions
  1. 36 7
      lib/Gedmo/Timestampable/TimestampableListener.php

+ 36 - 7
lib/Gedmo/Timestampable/TimestampableListener.php

@@ -3,7 +3,9 @@
 namespace Gedmo\Timestampable;
 
 use Doctrine\Common\EventArgs,
-    Gedmo\Mapping\MappedEventSubscriber;
+    Gedmo\Mapping\MappedEventSubscriber,
+    Doctrine\Common\NotifyPropertyChanged,
+    Gedmo\Exception\UnexpectedValueException;
 
 /**
  * The Timestampable listener handles the update of
@@ -66,7 +68,9 @@ class TimestampableListener extends MappedEventSubscriber
                     foreach ($config['update'] as $field) {
                         if (!isset($changeSet[$field])) { // let manual values
                             $needChanges = true;
-                            $meta->getReflectionProperty($field)->setValue($object, $ea->getDateValue($meta, $field));
+                            //$meta->getReflectionProperty($field)->setValue($object, $ea->getDateValue($meta, $field));
+                            $this->updateField($object, $ea, $meta, $field);
+
                         }
                     }
                 }
@@ -90,7 +94,9 @@ class TimestampableListener extends MappedEventSubscriber
                             if (isset($trackedChild)) {
                                 $changingObject = $changes[1];
                                 if (!is_object($changingObject)) {
-                                    throw new \Gedmo\Exception\UnexpectedValueException("Field - [{$field}] is expected to be object in class - {$meta->name}");
+                                    throw new UnexpectedValueException(
+                                        "Field - [{$field}] is expected to be object in class - {$meta->name}"
+                                    );
                                 }
                                 $objectMeta = $om->getClassMetadata(get_class($changingObject));
                                 $trackedChild instanceof Proxy && $om->refresh($trackedChild);
@@ -102,8 +108,9 @@ class TimestampableListener extends MappedEventSubscriber
 
                             if ($options['value'] == $value) {
                                 $needChanges = true;
-                                $meta->getReflectionProperty($options['field'])
-                                    ->setValue($object, $ea->getDateValue($meta, $options['field']));
+                                //$meta->getReflectionProperty($options['field'])
+                                //    ->setValue($object, $ea->getDateValue($meta, $options['field']));
+                                $this->updateField($object, $ea, $meta, $options['field']);
                             }
                         }
                     }
@@ -134,7 +141,8 @@ class TimestampableListener extends MappedEventSubscriber
             if (isset($config['update'])) {
                 foreach ($config['update'] as $field) {
                     if ($meta->getReflectionProperty($field)->getValue($object) === null) { // let manual values
-                        $meta->getReflectionProperty($field)->setValue($object, $ea->getDateValue($meta, $field));
+                        //$meta->getReflectionProperty($field)->setValue($object, $ea->getDateValue($meta, $field));
+                        $this->updateField($object, $ea, $meta, $field);
                     }
                 }
             }
@@ -142,7 +150,8 @@ class TimestampableListener extends MappedEventSubscriber
             if (isset($config['create'])) {
                 foreach ($config['create'] as $field) {
                     if ($meta->getReflectionProperty($field)->getValue($object) === null) { // let manual values
-                        $meta->getReflectionProperty($field)->setValue($object, $ea->getDateValue($meta, $field));
+                        //$meta->getReflectionProperty($field)->setValue($object, $ea->getDateValue($meta, $field));
+                        $this->updateField($object, $ea, $meta, $field);
                     }
                 }
             }
@@ -156,4 +165,24 @@ class TimestampableListener extends MappedEventSubscriber
     {
         return __NAMESPACE__;
     }
+
+    /**
+     * Updates a field
+     *
+     * @param $object
+     * @param $ea
+     * @param $meta
+     * @param $field
+     */
+    protected function updateField($object, $ea, $meta, $field)
+    {
+        $property = $meta->getReflectionProperty($field);
+        $oldValue = $property->getValue($object);
+        $newValue = $ea->getDateValue($meta, $field);
+        $property->setValue($object, $newValue);
+        if ($object instanceof NotifyPropertyChanged) {
+            $uow = $ea->getObjectManager()->getUnitOfWork();
+            $uow->propertyChanged($object, $field, $oldValue, $newValue);
+        }
+    }
 }