فهرست منبع

add change field to any value (issue #412)

Ivan Borzenkov 12 سال پیش
والد
کامیت
585e1b0ca4

+ 3 - 3
lib/Gedmo/Timestampable/Mapping/Driver/Annotation.php

@@ -64,13 +64,13 @@ class Annotation extends AbstractAnnotationDriver
                     throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
                 }
                 if ($timestampable->on == 'change') {
-                    if (!isset($timestampable->field) || !isset($timestampable->value)) {
-                        throw new InvalidMappingException("Missing parameters on property - {$field}, field and value must be set on [change] trigger in class - {$meta->name}");
+                    if (!isset($timestampable->field)) {
+                        throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
                     }
                     $field = array(
                         'field' => $field,
                         'trackedField' => $timestampable->field,
-                        'value' => $timestampable->value
+                        'value' => $timestampable->value !== null ? $timestampable->value : false,
                     );
                 }
                 // properties are unique and mapper checks that, no risk here

+ 3 - 3
lib/Gedmo/Timestampable/Mapping/Driver/Xml.php

@@ -67,13 +67,13 @@ class Xml extends BaseXml
                     }
 
                     if ($this->_getAttribute($data, 'on') == 'change') {
-                        if (!$this->_isAttributeSet($data, 'field') || !$this->_isAttributeSet($data, 'value')) {
-                            throw new InvalidMappingException("Missing parameters on property - {$field}, field and value must be set on [change] trigger in class - {$meta->name}");
+                        if (!$this->_isAttributeSet($data, 'field')) {
+                            throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
                         }
                         $field = array(
                             'field' => $field,
                             'trackedField' => $this->_getAttribute($data, 'field'),
-                            'value' => $this->_getAttribute($data, 'value')
+                            'value' => $this->_isAttributeSet($data, 'value') ? $this->_getAttribute($data, 'value') : false,
                         );
                     }
                     $config[$this->_getAttribute($data, 'on')][] = $field;

+ 3 - 3
lib/Gedmo/Timestampable/Mapping/Driver/Yaml.php

@@ -59,13 +59,13 @@ class Yaml extends File implements Driver
                     }
 
                     if ($mappingProperty['on'] == 'change') {
-                        if (!isset($mappingProperty['field']) || !isset($mappingProperty['value'])) {
-                            throw new InvalidMappingException("Missing parameters on property - {$field}, field and value must be set on [change] trigger in class - {$meta->name}");
+                        if (!isset($mappingProperty['field'])) {
+                            throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
                         }
                         $field = array(
                             'field' => $field,
                             'trackedField' => $mappingProperty['field'],
-                            'value' => $mappingProperty['value']
+                            'value' => isset($mappingProperty['value']) ? $mappingProperty['value'] : false,
                         );
                     }
                     $config[$mappingProperty['on']][] = $field;

+ 6 - 1
lib/Gedmo/Timestampable/Timestampable.php

@@ -32,7 +32,12 @@ interface Timestampable
      * dates which should be updated on changed "property" 
      * value and become equal to given "value"
      */
-    
+
+    /**
+     * @gedmo:Timestampable(on="change", field="field")
+     * dates which should be updated on changed "property"
+     */
+
     /**
      * example
      * 

+ 1 - 1
lib/Gedmo/Timestampable/TimestampableListener.php

@@ -105,7 +105,7 @@ class TimestampableListener extends MappedEventSubscriber
                                 $value = $changes[1];
                             }
 
-                            if ($options['value'] == $value) {
+                            if ($options['value'] == $value || $options['value'] === false) {
                                 $needChanges = true;
                                 $this->updateField($object, $ea, $meta, $options['field']);
                             }

+ 73 - 0
tests/Gedmo/Timestampable/ChangeTest.php

@@ -0,0 +1,73 @@
+<?php
+
+namespace Gedmo\Timestampable;
+
+use Doctrine\Common\EventManager;
+use Tool\BaseTestCaseORM;
+use Doctrine\Common\Util\Debug,
+    Timestampable\Fixture\TitledArticle;
+
+/**
+ * These are tests for Timestampable behavior
+ *
+ * @author Ivan Borzenkov <ivan.borzenkov@gmail.com>
+ * @package Gedmo.Timestampable
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class ChangeTest extends BaseTestCaseORM
+{
+    const FIXTURE = "Timestampable\\Fixture\\TitledArticle";
+
+    protected function setUp()
+    {
+        parent::setUp();
+
+        $evm = new EventManager;
+        $evm->addEventSubscriber(new TimestampableListener);
+
+        $this->getMockSqliteEntityManager($evm);
+    }
+
+    public function testChange()
+    {
+        $test = new TitledArticle();
+        $test->setTitle('Test');
+        $test->setText('Test');
+
+        $date = new \DateTime('now');
+        $this->em->persist($test);
+        $this->em->flush();
+        $this->em->clear();
+
+        $test = $this->em->getRepository(self::FIXTURE)->findOneByTitle('Test');
+        $test->setTitle('New Title');
+        $this->em->persist($test);
+        $this->em->flush();
+        $this->em->clear();
+        //Changed
+        $this->assertEquals(
+            $date->format('Y-m-d H:i:s'),
+            $test->getChtitle()->format('Y-m-d H:i:s')
+        );
+        sleep(1);
+        $test = $this->em->getRepository(self::FIXTURE)->findOneByTitle('New Title');
+        $test->setText('New Text');
+        $this->em->persist($test);
+        $this->em->flush();
+        $this->em->clear();
+        //Not Changed
+        $this->assertEquals(
+            $date->format('Y-m-d H:i:s'),
+            $test->getChtitle()->format('Y-m-d H:i:s')
+        );
+        $date = new \DateTime('now');
+    }
+
+    protected function getUsedEntityFixtures()
+    {
+        return array(
+            self::FIXTURE,
+        );
+    }
+}

+ 107 - 0
tests/Gedmo/Timestampable/Fixture/TitledArticle.php

@@ -0,0 +1,107 @@
+<?php
+namespace Timestampable\Fixture;
+
+use Gedmo\Timestampable\Timestampable;
+use Gedmo\Mapping\Annotation as Gedmo;
+use Doctrine\ORM\Mapping as ORM;
+
+/**
+ * @ORM\Entity
+ */
+class TitledArticle implements Timestampable
+{
+    /**
+     * @ORM\Id
+     * @ORM\GeneratedValue
+     * @ORM\Column(type="integer")
+     */
+    private $id;
+
+    /**
+     * @ORM\Column(name="title", type="string", length=128)
+     */
+    private $title;
+
+    /**
+     * @ORM\Column(name="text", type="string", length=128)
+     */
+    private $text;
+
+    /**
+     * @var \DateTime $updated
+     *
+     * @ORM\Column(name="chtext", type="datetime", nullable=true)
+     * @Gedmo\Timestampable(on="change", field="text")
+     */
+    private $chtext;
+
+    /**
+     * @var \DateTime $chtitle
+     *
+     * @ORM\Column(name="chtitle", type="datetime", nullable=true)
+     * @Gedmo\Timestampable(on="change", field="title")
+     */
+    private $chtitle;
+
+    /**
+     * @param \DateTime $chtext
+     */
+    public function setChtext($chtext)
+    {
+        $this->chtext = $chtext;
+    }
+
+    /**
+     * @return \DateTime
+     */
+    public function getChtext()
+    {
+        return $this->chtext;
+    }
+
+    /**
+     * @param \DateTime $chtitle
+     */
+    public function setChtitle($chtitle)
+    {
+        $this->chtitle = $chtitle;
+    }
+
+    /**
+     * @return \DateTime
+     */
+    public function getChtitle()
+    {
+        return $this->chtitle;
+    }
+
+    public function setId($id)
+    {
+        $this->id = $id;
+    }
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setText($text)
+    {
+        $this->text = $text;
+    }
+
+    public function getText()
+    {
+        return $this->text;
+    }
+
+    public function setTitle($title)
+    {
+        $this->title = $title;
+    }
+
+    public function getTitle()
+    {
+        return $this->title;
+    }
+}