我希望在更新特定字段时进行特定处理。
显而易见的方法是使用事件preUpdate执行此操作,并查看更新的字段。它工作正常......除了多对多字段。它触发事件,但ChangeSet为空。
/**
* @ORM\PreUpdate
*/
public function updateDate(PreUpdateEventArgs $event){
$changeSet = $event->getEntityChangeSet();
$res = "";
foreach($changeSet as $key => $change){
$line = $key." : ".$event->getOldValue($key)." || ".$event->getNewValue($key);
$res .= $line;
}
}
在$ res中,除了多对多字段外,我的所有字段都被修改。
另外,我正在尝试在侦听器中执行此操作,但我找不到如何提取从entityManager更新的字段。
谢谢。
更多信息:
更新实体的关系:
/**
* @var Status
*
* @ORM\ManyToMany(targetEntity="User", inversedBy="projectsSupervisor", cascade={"persist"})
* @ORM\JoinTable(name="projects_supervisors")
*/
protected $supervisors;
从另一方面来说:
/**
* @var Project
*
* @ORM\ManyToMany(targetEntity="Task", mappedBy="users")
*/
protected *tasks
Symfony版本:3.1.10
答案 0 :(得分:1)
无法跟踪对多对多关联所做的更改。见here:
忽略仅对关联的反面进行的更改。 确保更新双向关联的两侧(或在 至少是拥有方,从学说的角度来看)
此外,:: getEntityChangeSet()仅对常规字段有用,而不对关联有用。对于一对多关联,您可以使用$unitOfWork->getScheduledCollectionUpdates()
:
foreach ($uow->getScheduledCollectionUpdates() as $collectionUpdate) {
/** @var $collectionUpdate \Doctrine\ORM\PersistentCollection */
if ($collectionUpdate->getOwner() === $entity) {
// This entity has an association mapping which contains updates.
$collectionMapping = $collectionUpdate->getMapping();
print_r($collectionMapping); // Investigate this further
}
}
在我的github存储库中可以查看一个实际示例" DoctrineWatcher" exactly the same(第196 +)行。