原则 2 具有抽象基类的 LifecycleCallbacks 不会被调用

发布于 2024-12-03 07:39:27 字数 1195 浏览 0 评论 0 原文

我有这种情况:

抽象类:

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 继承的对象。这是一个错误,还是有原因?

I have this situation:

Abstract Class:

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();
    }
}

Concrete Class:

/**
 * @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.
}

Then i call it in my controller like this:

$user = $this->em->find('Entity\User', 1);
// i call some setters here like $user->setName('asd');
$this->em->flush();
die('end');

Everything works as expected, so the id field from the abstract class gets created for the User entity, i can access it etc.
The problem is, that the line "die('THIS POINT IS NEVER REACHED')" is never reached. (Note the @ORM\PreUpdate) This means that lifecycleCallbacks are not called on
inherited objects. Is this a bug, or is there a reason for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

胡大本事 2024-12-10 07:39:27

您的抽象基类必须注释为映射超类并包含HasLifecycleCallbacks注释。

更多信息:Doctrine 文档中的继承映射

/**
 * @ORM\MappedSuperclass
 * @ORM\HasLifecycleCallbacks
 */
abstract class AbstractBase
{
    [...]

    /**
     * @ORM\PreUpdate
     */
    public function setUpdatedAt()
    {
        $this->updatedAt = new \DateTime();
    }
}

/**
 * @ORM\Entity(repositoryClass="Entity\Repository\UserRepository")
 * @ORM\Table(name="users")
 */
class User extends AbstractBase
{
    // some fields, relations and setters/getters defined here, these all work as expected.
}

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\MappedSuperclass
 * @ORM\HasLifecycleCallbacks
 */
abstract class AbstractBase
{
    [...]

    /**
     * @ORM\PreUpdate
     */
    public function setUpdatedAt()
    {
        $this->updatedAt = new \DateTime();
    }
}

/**
 * @ORM\Entity(repositoryClass="Entity\Repository\UserRepository")
 * @ORM\Table(name="users")
 */
class User extends AbstractBase
{
    // some fields, relations and setters/getters defined here, these all work as expected.
}
挽袖吟 2024-12-10 07:39:27

您必须使用 @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.

暖阳 2024-12-10 07:39:27

虽然接受的答复对于一般情况是正确的,但在这种特殊情况(时间戳)中,您实际上想要使用学说扩展 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

情场扛把子 2024-12-10 07:39:27

具有 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.

凉世弥音 2024-12-10 07:39:27

也许我错了,但我不认为当你保留一个实体时不会触发 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.

〗斷ホ乔殘χμё〖 2024-12-10 07:39:27

也许你可以这个 问题报告作为参考如何设置注释?该测试用例似乎有效并且符合您的用例。

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.

故事与诗 2024-12-10 07:39:27

我认为你必须用 @ORM\HasLifecycleCallbacks 注释基类

文档

I think you have to annotate the base class with @ORM\HasLifecycleCallbacks

docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文