Symfony 3/Doctrine - 获取实体更改集中关联的更改

Dar*_*one 5 php associations symfony doctrine-orm

所以我已经知道我可以在preUpdate生命周期事件中对特定实体进行更改:

/**
 * Captures pre-update events.
 * @param PreUpdateEventArgs $args
 */
 public function preUpdate(PreUpdateEventArgs $args)
 {
     $entity = $args->getEntity();


     if ($entity instanceof ParentEntity) {
            $changes = $args->getEntityChangeSet();
     }
 }
Run Code Online (Sandbox Code Playgroud)

但是,有没有办法同时获得任何相关实体的更改?例如,假设ParentEntity有这样的关系设置:

/**
 * @ORM\OneToMany(targetEntity="ChildEntity", mappedBy="parentEntity", cascade={"persist", "remove"})
 */
 private $childEntities;
Run Code Online (Sandbox Code Playgroud)

并且ChildEntity还有:

/**
 * @ORM\OneToMany(targetEntity="GrandChildEntity", mappedBy="childEntity", cascade={"persist", "remove"})
 */
 private $grandChildEntities;
Run Code Online (Sandbox Code Playgroud)

有没有办法让在所有相关的变化preUpdateParentEntity

rpg*_*600 8

OneToMany或ManyToMany关系中的所有关联实体都显示为Doctrine\ORM\PersistentCollection.

看一下PersistentCollection的API,它有一些有趣的公共方法,即使它们被标记为INTERNAL:https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/PersistentCollection.php#L308

例如,您可以检查您的集合是否为脏,这意味着其状态需要与数据库同步.然后,您可以检索已从集合中删除或插入其中的实体.

if ($entity->getChildEntities()->isDirty()) {
    $removed = $entity->getChildEntities()->getDeleteDiff();
    $inserted = $entity->getChildEntities()->getInsertDiff();
}
Run Code Online (Sandbox Code Playgroud)

此外,您可以在从数据库中获取集合时获取集合的快照:$entity->getChildEntities()->getSnapshot();,这用于创建上面的差异.