原则 2 具有抽象基类的 LifecycleCallbacks 不会被调用
我有这种情况:
抽象类:
abstract class AbstractBase
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @var integer
*/
protected $id;
/**
* @ORM\Column(type="datetime", name="updated_at")
* @var \DateTime $updatedAt
*/
protected $updatedAt;
/**
* @ORM\PreUpdate
*/
public function setUpdatedAt()
{
die('THIS POINT IS NEVER REACHED');
$this->updatedAt = new \DateTime();
}
}
具体类:
/**
* @ORM\Entity(repositoryClass="Entity\Repository\UserRepository")
* @ORM\Table(name="users")
* @ORM\HasLifecycleCallbacks
*/
class User extends AbstractBase
{
// some fields, relations and setters/getters defined here, these all work as expected.
}
然后我在我的控制器中这样调用它:
$user = $this->em->find('Entity\User', 1);
// i call some setters here like $user->setName('asd');
$this->em->flush();
die('end');
一切都按预期工作,因此为用户实体创建了抽象类中的 id 字段,我可以访问它等。 问题是,“die('THIS POINT IS NEVER REACHED')”这一行永远不会到达。 (注意@ORM\PreUpdate)这意味着不调用lifecycleCallbacks 继承的对象。这是一个错误,还是有原因?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您的抽象基类必须注释为映射超类并包含HasLifecycleCallbacks注释。
更多信息:Doctrine 文档中的继承映射。
Your abstract base class has to be anotated as Mapped Superclasses and include the HasLifecycleCallbacks-Annotation.
Further Information: Inheritance Mapping in the Doctrine Documentation.
您必须使用
@ORM\HasLifecycleCallbacks
注释基类,并使用@ORM\preUpdate
注释函数您有一个拼写错误(PreUpdate 应该是 preUpdate),preUpdate 也不是' t 在创建时调用(仅在更新时调用)。因此,如果您希望它在创建时也被触发,您应该添加@ORM\prePersist。
You have to annotate the base class with
@ORM\HasLifecycleCallbacks
, and the function with@ORM\preUpdate
You have a typo (PreUpdate should be preUpdate), also preUpdate isn't called on creation (only on update). So if you want it also be triggered on creation, you should add
@ORM\prePersist
.虽然接受的答复对于一般情况是正确的,但在这种特殊情况(时间戳)中,您实际上想要使用学说扩展 Timestampable,如此处所述 扩展 FOSUserBundle 用户实体时的生命周期回调问题
While the accepted reply is correct for the general case, in this particular case (timestamp) you actually want to use the doctrine extension Timestampable as explained for example here Lifecycle Callback Issue When Extending FOSUserBundle User Entity
具有 HasLifecycleCallbacks 的 MappedSuperclass 与其子实体位于相同的命名空间或目录中,这一点很重要。
当 MappedSuperclass 位于一个目录(模型)中而实体位于另一个目录(实体)中时,我遇到了生命周期回调问题。将 MappedSuperclass 放在与实体(Entity)相同的目录中解决了这个问题。
It is important that the MappedSuperclass with HasLifecycleCallbacks is in the same namespace or directory as their child Entities.
I had problems with life cycle callbacks when the MappedSuperclass was in one directory (Model) while the Entities were in another (Entity). Putting the MappedSuperclass in the same directory as the Entities (Entity) solved the issue.
也许我错了,但我不认为当你保留一个实体时不会触发 preUpdate。你应该有一个@prePersist。
http://www.doctrine-project.org/docs /orm/2.0/en/reference/events.html
但我仍然不确定这是否有效,但你可以尝试一下。否则,一种解决方法是覆盖 setUpdatedAt 函数并仅调用其父函数,但这有点难看。
希望@prePersist 对您有帮助。
Maybe i'm wrong but I don't think preUpdate isn't triggered when you persist an entity. You should have a @prePersist.
http://www.doctrine-project.org/docs/orm/2.0/en/reference/events.html
But still then i'm not sure this is going to work but you could try that. Else a workaround would be to overwrite the setUpdatedAt function and just call his parent one but that's a bit ugly.
Hope the @prePersist helps for you.
也许你可以这个 问题报告作为参考如何设置注释?该测试用例似乎有效并且符合您的用例。
Perhaps you could this issue report as a reference how to setup your annotations? The testcase seems to be valid and matches your use case.
我认为你必须用 @ORM\HasLifecycleCallbacks 注释基类
文档
I think you have to annotate the base class with @ORM\HasLifecycleCallbacks
docs