原则:事件侦听器中的多对多关联对象被删除 [英] Doctrine: many-to-many get removed associated object inside an event listener
问题描述
我有很多这样的协会
Entity Car
{
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="User", inversedBy="cars", fetch="EXTRA_LAZY")
* @ORM\JoinTable(
* name="cars_users",
* joinColumns={
* @ORM\JoinColumn(name="car_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* }
* )
*/
protected $users;
/**
* @param $user
*/
public function removeUser(User $user)
{
if (!$this->users->contains($user)) {
return;
}
$this->users->removeElement($user);
$user->removeCar($this);
return $this;
}
}
Entity User
{
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Car", mappedBy="users")
*/
protected $cars;
}
在我的控制器中,我有
$carA = getRandomCar();
$userB = getAUserThatBelongToCarA();
//remove association between Car A and User B
$carA->removesUser($userB);
doctrineUpdateCar($carA);
我有一个学说侦听器 CarListener
m试图找出在监听器中是否删除了 User
并告诉是哪个 User
。
I have a Doctrine Listener CarListener
i'm trying to find out if a User
has been removed inside that listener and tell which User
is that.
我尝试了 preUpdate
postUpdate
postFlush
,但找不到在 CarListener
内获取 $ userB
对象的方法
i tried preUpdate
postUpdate
postFlush
but couldn't figure out a way to get $userB
object inside the CarListener
推荐答案
事件
我建议您使用 onflush 事件,以使监听者/订阅者监听在计算更改之后但在对数据库执行任何操作之前将其调度到。这使之成为理想的选择,因为您可以访问所有即将进行的更改,但实际上尚未将所有更改都写入数据库(因此您可以选择中止)。
Event
I'd suggest you use the onflush event to have the listener/subscriber listen to, as it is dispatched after the changes are computed, but before any operations towards the database have been performed. This makes it ideal as you have potential access to all changes that are about to be made, but non of them have actually been written to the database yet (so you have the option to abort).
侦听器/订阅者传递了 OnFlushEventArgs
对象,您可以从该对象访问 UnitOfWork 。工作单元有5种方法来收集即将进行的更改:
The listener/subscriber gets passed an OnFlushEventArgs
object, from which you have access to the UnitOfWork. The Unit of work has 5 methods to gather the changes about to be made:
-
getScheduledEntityInsertions()
-
getScheduledEntityUpdates()
-
getScheduledEntityDeletions( )
-
getScheduledCollectionUpdates()
-
getScheduledCollectionDeletions()
getScheduledEntityInsertions()
getScheduledEntityUpdates()
getScheduledEntityDeletions()
getScheduledCollectionUpdates()
getScheduledCollectionDeletions()
您正在做的是从集合 Car :: $ users
中删除 User
并删除<$ c $来自集合 User :: cars
的c> Car 。
What you are doing is removing a User
from the collection Car::$users
and removing a Car
from the collection User::cars
.
所以没有实体实际上已删除,没有任何字段被更改。相反,您正在更改收藏集。换句话说,它是您要查看的 getScheduledCollectionUpdates()
。
So no entities are actually deleted, no fields are being changed. In stead you're changing collections. In other words, it's getScheduledCollectionUpdates()
you want to be looking at.
由...返回的集合 getScheduledCollectionUpdates()
是 PersistentCollection ,其中包含以下方法来告诉您发生了什么变化:
The collection(s) returned by getScheduledCollectionUpdates()
are an instance of PersistentCollection, which has these methods to tell you what has changed:
-
getInsertDiff()
-
getDeleteDiff()
getInsertDiff()
getDeleteDiff()
在您的情况下,您可以使用 getDeleteDiff()
来找出从集合中删除了哪个实体。
In your case you can use getDeleteDiff()
to find out which entity has been removed from the collection.
这篇关于原则:事件侦听器中的多对多关联对象被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!