I want to make fields updated_at
and created_at
in my Doctrine entities to update automatically.
In Ruby on Rails models there
$this->setCreatedAt(new \DateTime())
in __construct
method./**
* @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