更新createdAt& symfony3中自动更新的at字段(原则) [英] update createdAt & updatedAt fields automatically in symfony3 (doctrine)
问题描述
我有一个实体( Server ),应具有两个额外的字段: createdAt 和 updatedAt ( like cakephp )。我在 Server
实体上尝试过此操作:
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());
}
}
,但在更新过程中什么也没有发生。然后我进行了搜索,找到了这个 EventListener
解决方案。
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! ;)
推荐答案
检查您的实体上是否具有 HasLifecycleCallbacks
批注,例如:
Check you have the HasLifecycleCallbacks
annotation on your entity, as example:
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Server
希望获得帮助
这篇关于更新createdAt& symfony3中自动更新的at字段(原则)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!