Automatic values for updated_at, created_at in Doctrine

前端 未结 5 1321
感动是毒
感动是毒 2021-01-31 07:42

I want to make fields updated_at and created_at in my Doctrine entities to update automatically.

In Ruby on Rails models there

5条回答
  •  梦如初夏
    2021-01-31 08:02

    The most convinient solution for me is Timestampable feature of StofDoctrineExtensionsBundle.

    Simple configuration and later you are able to make fields createdAt and updatedAt of Entity filled out automatically by adding two simple annotations like:

    @Gedmo\Mapping\Annotation\Timestampable(on="create")

    and/or

    @Gedmo\Mapping\Annotation\Timestampable(on="update")

    e.g.

    /**
     * @var \DateTime
     * @Gedmo\Mapping\Annotation\Timestampable(on="create")
     * @Doctrine\ORM\Mapping\Column(type="datetime")
     */
    protected $createdAt;
    
    /**
     * @var \DateTime
     * @Gedmo\Mapping\Annotation\Timestampable(on="update")
     * @Doctrine\ORM\Mapping\Column(type="datetime")
     */
    protected $updatedAt;
    

    Without any redundant code in pure PHP.

提交回复
热议问题