Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony -> How to make created and modified fields dynamic with Doctrine?

Here my solution after this time ...

You just need to put this directly into your entity class :

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class MyEntity {

   //....

    public function __construct() {
        // we set up "created"+"modified"
        $this->setCreated(new \DateTime());
        if ($this->getModified() == null) {
            $this->setModified(new \DateTime());
        }
    }

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function updateModifiedDatetime() {
        // update the modified time
        $this->setModified(new \DateTime());
    }

    //....    
}

It works well actually


You can use StofDoctrineExtensionsBundle. This describes in symfony cookbook. It contains Timestampable behavior.

/**
 * @var datetime $created
 *
 * @Gedmo\Timestampable(on="create")
 * @ORM\Column(type="datetime")
 */
private $created;

/**
 * @var datetime $updated
 *
 * @Gedmo\Timestampable(on="update")
 * @ORM\Column(type="datetime")
 */
private $updated;

/**
 *
 * @ORM\PrePersist
 * @ORM\PreUpdate
 */
public function updatedTimestamps()
{
    $this->setModifiedAt(new \DateTime(date('Y-m-d H:i:s')));

    if($this->getCreatedAt() == null)
    {
        $this->setCreatedAt(new \DateTime(date('Y-m-d H:i:s')));
    }
}

You dont need to call in __constructor anything. Just create getter and setter properties created, modified and that is all.

If you set first setCreated() on every update you will update also created colum. So put first setModifedAt()


Two more examples (if you're using Yaml or Xml mapping):

Entity\Product:
  type: entity
  table: products
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    name:
      type: string
      length: 32
    created_at:
      type: date
      gedmo:
        timestampable:
          on: create
    updated_at:
      type: datetime
      gedmo:
        timestampable:
          on: update

And xml:

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping">

    <entity name="Mapping\Fixture\Xml\Timestampable" table="timestampables">
        <id name="id" type="integer" column="id">
            <generator strategy="AUTO"/>
        </id>

        <field name="created_at" type="datetime">
            <gedmo:timestampable on="create"/>
        </field>
        <field name="updated_at" type="datetime">
            <gedmo:timestampable on="update"/>
        </field>
    </entity>

</doctrine-mapping>

The other answers suggest using if statements (which means repeating your property names) and having property-setting logic in the constructor that might never be used.

Alternatively, you could have onAdd and onUpdate methods that are called when needed:

/**
 * @ORM\PrePersist
 */
public function onAdd()
{
    $this->setAdded(new DateTime('now'));
}

/**
 * @ORM\PrePersist
 * @ORM\PreUpdate
 */
public function onUpdate()
{
    $this->setUpdated(new DateTime('now'));
}