doctrine / doctrine-laminas-hydrator

Doctrine hydrators for Laminas applications
https://www.doctrine-project.org/projects/doctrine-laminas-hydrator.html
MIT License
33 stars 19 forks source link

Hydrator is trying to update instead of replace collection entities - can this be prevented? #80

Open Saeven opened 11 months ago

Saeven commented 11 months ago

Hello!

In trying to hydrate collection 'changes' (e.g., an update operation) - I've encountered an issue where adding and removing an element from this collection (via Fieldset) in a single operation causes duplicate key problems as Doctrine flushes.

I've created a repository to evidence the difficulties being encountered here: https://github.com/Saeven/CollectionHydration

Is there a means to achieve what I'm after the "Doctrine way", or is clearing and the repopulating the collection the intended behavior? This collection can contain thousands of entities in production, reissuing so many inserts seems wasteful...

I've got a band-aid for now, but was looking for 'the right way' to get this done!

Thank you. Alex

driehle commented 9 months ago

If you look at src/Strategy/AllowRemoveByReference, for instance, you should see that a diff between the two collections is build and the individual elements are indeed added or removed. So, technically, the collection is not rebuilt.

The comparison of objects is based on AbstractCollectionStrategy::compareObjects(), which uses spl_object_hash(). Could it be that your objects in fact are not identical, but different instances of the same entity?