不调用具有抽象基类的 Doctrine 2 LifecycleCallbacks [英] Doctrine 2 LifecycleCallbacks with abstract base class are not called

查看:14
本文介绍了不调用具有抽象基类的 Doctrine 2 LifecycleCallbacks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况:

抽象类:

abstract class AbstractBase
{
    /**
     * @ORMId
     * @ORMGeneratedValue
     * @ORMColumn(type="integer")
     * @var integer
     */
    protected $id;

    /**
     * @ORMColumn(type="datetime", name="updated_at")
     * @var DateTime $updatedAt
     */
    protected $updatedAt;

    /**
     * @ORMPreUpdate
     */
    public function setUpdatedAt()
    {
        die('THIS POINT IS NEVER REACHED');
        $this->updatedAt = new DateTime();
    }
}

具体类:

/**
 * @ORMEntity(repositoryClass="EntityRepositoryUserRepository")
 * @ORMTable(name="users")
 * @ORMHasLifecycleCallbacks
 */
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('EntityUser', 1);
// i call some setters here like $user->setName('asd');
$this->em->flush();
die('end');

一切都按预期工作,因此抽象类中的 id 字段为 User 实体创建,我可以访问它等.问题是,永远不会到达死('这个点从未达到')"这一行.(注意@ORMPreUpdate)这意味着不会调用lifecycleCallbacks继承的对象.这是一个错误,还是有原因?

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 @ORMPreUpdate) This means that lifecycleCallbacks are not called on inherited objects. Is this a bug, or is there a reason for this?

推荐答案

您的抽象基类必须被注释为映射超类,并包含 HasLifecycleCallbacks-注释.

Your abstract base class has to be anotated as Mapped Superclasses and include the HasLifecycleCallbacks-Annotation.

更多信息:教义文档中的继承映射.

/**
 * @ORMMappedSuperclass
 * @ORMHasLifecycleCallbacks
 */
abstract class AbstractBase
{
    [...]

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

/**
 * @ORMEntity(repositoryClass="EntityRepositoryUserRepository")
 * @ORMTable(name="users")
 */
class User extends AbstractBase
{
    // some fields, relations and setters/getters defined here, these all work as expected.
}

这篇关于不调用具有抽象基类的 Doctrine 2 LifecycleCallbacks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆