PreUpdate级联实体保留symfony 2.3 [英] PreUpdate cascade entity persist symfony 2.3

查看:75
本文介绍了PreUpdate级联实体保留symfony 2.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用PreUpdate HasLifecycleCallbacks时遇到一些问题。

I have a bit of problems with PreUpdate HasLifecycleCallbacks.

我有一个实体,比方说 A与实体 B具有OneToOne关系。
所以我有:

I have an entity, let say "A" with have a OneToOne relation with the entity "B". So I have:

/**
 * @ORM\Entity()
 * @ORM\HasLifecycleCallbacks
 */
 class A
{
    /**
     * @ORM\OneToOne(targetEntity="B", inversedBy="attrA", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="fieldB", referencedColumnName="id")
     */
    private $attrB;

    public function __construct()
    {
        $this->attrB = new B();
    }

    /**
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function updateSomthing(){
        //$gestor = fopen("/pruebitas.txt", "r");
        $this->attrB->setDate($this->getDate());
   }
}

B类为:

class B
{
    /**
     * @ORM\OneToOne(targetEntity="A", mappedBy="attrB")
     */
    private $attrA;
}

当我创建新实体A时,一切正常,问题是我更新了实体A,PreUpdate函数启动了(因为它在注释行中创建了文件),但是即使应更新B中的字段,实体B也不持久存在于数据库中。

When I create a new entity A, everything works fine, the problem is when I update the entity A, the PreUpdate function is fire, (because it creates the file in the commented line), but the entity B does not persist in the database, even if the field in B should be updated.

任何在PreUpdate上级联持久化的想法吗?

Any idea to cascade the persist on the PreUpdate??

谢谢!

推荐答案

使用 preFlush 代替



来自Doctrine文档 preUpdate 事件:


不允许在以下位置更改已更新实体的关联
此事件,因为Doctrine无法保证在刷新操作的这一点正确处理
参照完整性。

Changes to associations of the updated entity are never allowed in this event, since Doctrine cannot guarantee to correctly handle referential integrity at this point of the flush operation.

这是有道理的,因此在工作单元生成所有变更集之前,您需要对关联实体进行变更。这就是 preFlush 事件的目的。

That makes sense, so you need to do your changes to associated entities before all the changesets are genereated by the Unit of Work. That's what the preFlush event is for.


preFlush在EntityManager#flush上调用()。可以在监听器中安全地调用
EntityManager#flush()。

preFlush is called at EntityManager#flush() before anything else. EntityManager#flush() can be called safely inside its listeners.

只需替换您的 @带有 @ ORM\PreFlush 的ORM\PreUpdate 批注,它应该可以使用。

Simply replace your @ORM\PreUpdate annotation with @ORM\PreFlush and it should work.

preFlush 事件自Doctrine 2.2起可用。

The preFlush event is available since Doctrine 2.2.

文档文档:事件-preFlush

Doctrine错误跟踪器: preFlush事件和生命周期回调

这篇关于PreUpdate级联实体保留symfony 2.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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