我有两个实体Foo
和Bar
,Bar
与Foo<有一个
称为 manyToOne
关联$foo
和 Foo
具有逆关联,即一个名为 $bars
的 oneToMany
关联,并且 .并且任何一侧都没有定义级联
操作。现在我的问题是这样的,如果我获取一个 Foo
实例 $foo
,然后使用 $bar- 创建并保留一些
,并将它们保存到Bar
实例>foo = $foo实体管理器
并刷新它,然后如果我从存储库,$foo->bars
不会包含我刚刚创建的 Bar
实例(除非我调用 $entityManager->detach($foo)
首先。
另一方面,如果我通过调用 $em->remove($bar)
和 删除与 $foo
相关的一些 Bar
实体刷新实体管理器
,那么如果我从存储库重新获取$foo
,我删除的Bar
实例将不会出现在不再是 $bars
属性。我将用一些代码进行更多解释。
$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.
我想知道这是否是预期的行为。
最佳答案
想发表评论,但根据我的声誉仍然无法发表评论:-(
但我很确定这回答了您的问题:“更新双向关联时,Doctrine 仅检查两侧之一是否有这些更改。这称为关联的拥有方。”
检查关联文档:http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-associations.html尤其是http://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork-associations.html
关于mysql - 教义2 : Different behaviours on removing and persisting related Entities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31913664/