Procházet zdrojové kódy

Merge pull request #245 from prolic/master

Timestampable now works with change tracking policy notify
Gediminas Morkevicius před 13 roky
rodič
revize
6061df4157

+ 1 - 0
lib/Gedmo/Timestampable/Mapping/Event/Adapter/ORM.php

@@ -22,6 +22,7 @@ final class ORM extends BaseAdapterORM implements TimestampableAdapter
      */
     public function getDateValue($meta, $field)
     {
+        $mapping = $meta->getFieldMapping($field);
         if (isset($mapping['type']) && $mapping['type'] == 'zenddate') {
             return new \Zend_Date();
         }

+ 31 - 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,8 @@ 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));
+                            $this->updateField($object, $ea, $meta, $field);
+
                         }
                     }
                 }
@@ -90,7 +93,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 +107,7 @@ class TimestampableListener extends MappedEventSubscriber
 
                             if ($options['value'] == $value) {
                                 $needChanges = true;
-                                $meta->getReflectionProperty($options['field'])
-                                    ->setValue($object, $ea->getDateValue($meta, $options['field']));
+                                $this->updateField($object, $ea, $meta, $options['field']);
                             }
                         }
                     }
@@ -134,7 +138,7 @@ 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));
+                        $this->updateField($object, $ea, $meta, $field);
                     }
                 }
             }
@@ -142,7 +146,7 @@ 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));
+                        $this->updateField($object, $ea, $meta, $field);
                     }
                 }
             }
@@ -156,4 +160,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);
+        }
+    }
 }