mysql - 教义2 : Different behaviours on removing and persisting related Entities

标签 mysql doctrine-orm

我有两个实体FooBarBarFoo<有一个manyToOne关联 称为 $fooFoo 具有逆关联,即一个名为 $barsoneToMany 关联,并且 .并且任何一侧都没有定义级联操作。现在我的问题是这样的,如果我获取一个 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/

相关文章:

php - 使用 Doctrine 和 ZF2 进行分页

php - 学说 2 可排序

php - 在 MySQL 中存储日期和时间的更好方法是什么?

php - 试图创建数据库,但出现 mysql 错误 [Syfony2]

MySQL - 选择字符串的前 10 个字节

mysql - MySQL基于列的唯一值获取唯一行

doctrine-orm - 没有为类 Doctrine\ORM\PersistentCollection 定义实体管理器

php - 在 Doctrine 中使用设置为 NULL 的唯一 ID 创建的实体对象

php - 从 php 表行传递参数

php - 计算 sql 结果中的重复项并用 PHP 显示