The cause of this problem is twofold:
1) When the clear() method is called on Doctrine\ORM\PersistentCollection , it will be:
- clear your internal set of objects.
- calling
scheduleCollectionDeletion() on Doctrine\ORM\UnitOfWork . - take a new picture of yourself.
Number 2 is the reason your collection is displayed in $uow->getScheduledCollectionDeletions() (and not in $uow->getScheduledCollectionUpdates() ). Number 3 is the reason you cannot determine what was in the collection before it was cleaned.
2) When using the Symfony2 Form component, in particular the ChoiceType or CollectionType types in combination with the multiple option, this clear() method is called when all objects must be removed from the collection.
This is due to the addition of MergeDoctrineCollectionListener : https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php#L55
This is done as an optimization: it cleans the collection faster in this way, instead of checking which objects should be removed from it.
I can think of two possible solutions :
1) Create a fork symfony/symfony and implement the option so as not to add a MergeDoctrineCollectionListener . Perhaps something like no_clear to prevent the addition of a listener. This will not break BC and solve your problem, because the clear() method of the collection will not be called when all objects have to be deleted.
2) Redesign your meter. You may also listen to the OnLoad event, which can count the number of objects in the collection at the moment it is retrieved from db. Thus, your OnFlush listener can use this number to find out how many objects were removed from the collection when it was cleared.
source share