update createdAt & updatedAt fields automatically

2019-07-14 04:34发布

I have an entity (Server) that should have two extra fields: createdAt and updatedAt (like cakephp). I tried this on Server entity:

/**
 * @ORM\PrePersist
 * @ORM\PreUpdate
 */
public function updateTimestamp()
    {
        $this->setModifiedAt(new \DateTime());
        if($this->getCreatedAt() == null){
            $this->setCreatedAt(new \DateTime());
        }
    }

but nothing happened during update process. Then I searched about it and there was this EventListener solution.

I couldn't fix it by that too.

is there any solution for this problem, please provide a step by step solution.

thank you a lot and sorry for bad English! ;)

1条回答
神经病院院长
2楼-- · 2019-07-14 05:01

Check you have the HasLifecycleCallbacks annotation on your entity, as example:

/**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks
 */
class Server

Hope this help

查看更多
登录 后发表回答