Answer the question
In order to leave comments, you need to log in
How to get Doctrine entities removed from collection?
Friends, it became necessary to keep track of which entities were removed from the associated many-to-many collection, but then I came across a strange bug (maybe a feature?).
Given: there are 2 entities - User and Car. There is a common many-to-many relationship between them. If you do this:
// echo $user->getCars()->count(); = 1
$car = $user->getCars()->get(0);
$user->removeCar($car);
var_dump($user->getCars()->getDeleteDiff()); // тут есть удаленный элемент
// echo $user->getCars()->count(); = 1
$car = $user->getCars()->get(0);
$user->getCars()->clear();
$user->addCar($car);
$user->removeCar($car);
var_dump($user->getCars()->getDeleteDiff()); // пустой массив
$user->getCars()->clear();
$car = $user->getCars()->get(0);
$user->getCars()->clear();
$user->addCar($car);
var_dump($user->getCars()->getInsertDiff()); // тут есть добавленный элемент
Answer the question
In order to leave comments, you need to log in
The answers to your questions, obviously, lie in the Doctrine sources:
It can be seen that the behavior of the method clear()
depends on which side of the association you clear. You can also see that if you work with the owner side of the association, then Doctrine creates a new snapshot.
From the code of the getInsertDiff and getDeleteDiff methods, it can be seen that the difference is calculated by comparing the snapshot with the current contents of the collection, so it is obvious that if a new snapshot is created, the difference will be an empty array.
The easiest way to check if Symfony itself is calling a method clear()
is to set a breakpoint there.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question