如何在doctrine2中合并反序列化的对象图时自动删除孤立?

sys*_*tho 7 orm merge orphan symfony doctrine-orm

我正在使用Symfony2和Doctrine2来构建后端API.此API将接收数据库中CRUD的对象图.

我正在使用伟大的JMSSerializer包来将对象的json编码图反序列化为分离的已分离的图表,然后我想简单地将该图直接合并到数据库中.

在我的代码中,我有2个实体:父和子.OneToMany(cascade={"all"}, orphanRemoval=true)在Parent中定义了一种关系.

在我的数据库中,我有一个id为1的Parent行,它有3个id为1,2,3的子节点.

我想写一些类似的东西(我这里没有使用反序列化来简化示例):

$parent = new Parent(); $parent->id = 1  // detached entity
$existing_child = new Child(); $child->id = 2 // detached entity
$new_child = new Child(); // new entity
$parent->addChild($existing_child);
$parent->addChild($new_child);

$em = $this->getDoctrine()
    ->getEntityManager();
$em->merge($parent);

$em->flush();
Run Code Online (Sandbox Code Playgroud)

这样做,我的新孩子创建得很好,现有的孩子会更新,但旧孩子(1和3)不会被删除.

如果我错过了什么,有人可以帮助我吗?