Home » Php » php – Automatic values for updated_at, created_at in Doctrine

php – Automatic values for updated_at, created_at in Doctrine

Posted by: admin April 23, 2020 Leave a comment

Questions:

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

In Ruby on Rails models there are 2 fields: updated_at and created_at.

Description can be found here: http://guides.rubyonrails.org/migrations.html#migration-overview:

The timestamps macro adds two columns, created_at and updated_at. These special columns are automatically managed by Active Record if they exist.

Can I enable similar functionality in Doctrine 2?

How to&Answers:
  1. You can call $this->setCreatedAt(new \DateTime()) in __construct method.
  2. You can use Life Cycle Callbacks
/**
 * @ORM\PrePersist
 * @ORM\PreUpdate
*/
public function updatedTimestamps(): void
{
    $this->setUpdatedAt(new \DateTime('now'));    
    if ($this->getCreatedAt() === null) {
        $this->setCreatedAt(new \DateTime('now'));
    }
}

And don’t forget to add into entity class notation:
@ORM\HasLifecycleCallbacks

Answer:

This is another option if you would ever want to handle them separately.

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="person")
 * @ORM\HasLifecycleCallbacks
 */
class Person
{
    ..........

    /**
     * @var datetime $created
     *
     * @ORM\Column(type="datetime")
     */
    protected $created;

    /**
     * @var datetime $updated
     * 
     * @ORM\Column(type="datetime", nullable = true)
     */
    protected $updated;


    /**
     * Gets triggered only on insert

     * @ORM\PrePersist
     */
    public function onPrePersist()
    {
        $this->created = new \DateTime("now");
    }

    /**
     * Gets triggered every time on update

     * @ORM\PreUpdate
     */
    public function onPreUpdate()
    {
        $this->updated = new \DateTime("now");
    }

    ..........
}

Answer:

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.

Answer:

Also u cat make trait, like this

<?php

namespace App\Entity\Traits;

use DateTime;
use DateTimeInterface;
use Exception;

/**
 * Trait TimeStampableTrait
 * @package App\Entity\Trait
 */
trait TimeStampableTrait
{
    /**
     * @ORM\Column(type="datetime")
     */
    private $createdAt;

    /**
     * @ORM\Column(type="datetime")
     */
    private $updatedAt;

    /**
     * @return DateTimeInterface|null
     * @throws Exception
     */
    public function getCreatedAt(): ?DateTimeInterface
    {
        return $this->createdAt ?? new DateTime();
    }

    /**
     * @param DateTimeInterface $createdAt
     * @return $this
     */
    public function setCreatedAt(DateTimeInterface $createdAt): self
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * @return DateTimeInterface|null
     */
    public function getUpdatedAt(): ?DateTimeInterface
    {
        return $this->updatedAt ?? new DateTime();
    }

    /**
     * @param DateTimeInterface $updatedAt
     * @return $this
     */
    public function setUpdatedAt(DateTimeInterface $updatedAt): self
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function updateTimestamps(): void
    {
        $now = new DateTime();
        $this->setUpdatedAt($now);
        if ($this->getId() === null) {
            $this->setCreatedAt($now);
        }
    }
}

Add this trait in your entity (and don’t forget @ORM\HasLifecycleCallbacks notation):

<?php

namespace App\Entity;

use App\Entity\Traits\TimeStampableTrait;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="App\Repository\MyEntityRepository")
 * @ORM\HasLifecycleCallbacks
 */
class MyEntity
{
    use TimeStampableTrait;
}