- You can call
$this->setCreatedAt(new \DateTime())in__constructmethod. - 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