Doctrine - preUpdate在prePersist执行时调用但不更改相关的实体属性

时间:2014-12-12 08:51:18

标签: symfony doctrine lifecycle

我对Doctrine有点新鲜,我发现LifeCycleCallback非常有用但是当我实现时我发现这个缺乏:

我有一位作者 - >预订一对多关系,我将prePersistpreUpdate lifecyclecallback事件放在图书实体中,以便可以更改作者实体上的属性。但我发现prePersist工作正常,但preUpdate无法对相关实体进行更改。我在这里遗漏了什么,或者我是否应该注册Event Listener & Subscriber以更改相关的实体属性?

注意:我正在关注Symfony doc on simple lifecycle callback

部分代码段:

书籍映射到作者&生命周期回调:

<many-to-one field="author" target-entity="Author" inversed-by="books">
    <join-columns>
        <join-column name="author_id" referenced-column-name="id"/>
    </join-columns>
</many-to-one>
<lifecycle-callbacks>
    <lifecycle-callback type="prePersist" method="prePersistAction" />
    <lifecycle-callback type="preUpdate" method="preUpdateAction" />
</lifecycle-callbacks>

作者映射到Book

<one-to-many field="books" target-entity="Book" mapped-by="author"></one-to-many>

在Book实体上,我添加了两个响应事件回调的方法:

/**
 * On prePersist Event. 
 */
public function prePersistAction()
{
    $this->name = 'Some new name here';
    $this->author->setName('new Author name as per book');
}
/**
 * On preUpdate Event.
 */
public function preUpdateAction()
{
    $this->name = 'Some new name here';
    $this->author->setName('new Author name as per book');
}

0 个答案:

没有答案