教义2:删除和坚持相关实体的行为不同 [英] Doctrine2: Different behaviours on removing and persisting related Entities

查看:150
本文介绍了教义2:删除和坚持相关实体的行为不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体 Foo Bar Bar 有一个 manyToOne 关联到 Foo 调用 $ foo Foo 具有反向关联, oneToMany 关联称为 $ bars 和。而且在任何一方都定义了 no cascade 操作。现在我的问题是这样的,如果我获取一个 Foo 实例 $ foo ,然后创建并持续一些 Bar 具有 $ bar-> foo = $ foo 的实例,并将其持续到实体管理器 flush ,那么如果我从存储库中重新获取 $ foo $ foo-> bar 不会包含刚才创建的 Bar 实例(除非我调用$ code> $ entityManager-> detach($ foo )
另一方面,如果我删除了一些 Bar $ foo 通过调用 $ em-> remove($ bar) flush 实体管理器,那么如果我从存储库中重新获取 $ foo ,我删除的实例将不会在 $ bars 属性中,我将再解释一些代码。

I have two Entities Foo and Bar, Bar has a manyToOne association to Foo called $foo and Foo has the inverse association, a oneToMany association called $bars and . And there are no cascade operations defined on any side. Now my problem is this, if I fetch one Foo instance $foo, then create and persist some Bar instances with $bar->foo = $foo, and persist them to the Entity Manager and flush it, then if I refetch $foo from the Repository, $foo->bars won't contain the Bar instances I just created (unless I call $entityManager->detach($foo) first. On the other hand if I remove some Bar entities related to $foo by calling $em->remove($bar) and flush the Entity Manager, then if I refetch $foo from the Repository, the Bar instances I removed won't be in the $bars property anymore. I'll explain more with some code.

$foo = $fooRepo->find($id);
$bar = new Bar($foo);
$entityManager->persist($bar);
$entityManager->flush();
$foo = $fooRepo->find($id);
var_dump($foo->getBars()); // $foo->bars won't contain $bar unless I detach $foo first







// Now the other case
$foo = $fooRepo->find($id);
$bars = $foo->getBars();
$bar = $bars[0];
$entityManager->remove($bar);
$entityManager->flush();
$foo = $fooRepo->find($id);
var_dump($foo->getBars()); // this time $foo->bars will reflect the changes we made.  

我想知道这是否是行为。

I want to know if this is expected Behavior.

推荐答案

想要评论,但仍然不能按照我的声誉: - (

Wanted to comment, but still can't according to my reputation :-(

但我很确定这回答了你的问题:当一个双向协议被更新时,Doctrine只会检查双方之间的这些变化,这被称为协会的拥有方。

But I am pretty sure this answers your question: "When a bidirectional assocation is updated, Doctrine only checks on one of both sides for these changes. This is called the owning side of the association."

查看有关协会的文档: http:// doctrine- orm.readthedocs.org/en/latest/reference/working-with-associations.html ,特别是 http://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork-associations.html

Check the documentation for associations: http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-associations.html and especially http://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork-associations.html

这篇关于教义2:删除和坚持相关实体的行为不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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