不调用具有抽象基类的 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.
}
相关文章