DDC-1283: Possible issue with PersistentCollection#getDelete/InsertDiff() #1617

Open
opened 2026-01-22 13:20:01 +01:00 by admin · 6 comments
Owner

Originally created by @doctrinebot on GitHub (Jul 21, 2011).

Originally assigned to: @guilhermeblanco on GitHub.

Jira issue originally created by user darkangel:

Using the following code, when you go from (1, 2) to (1), (2) is deleted as expected. However, if you go from (1, 2) to (2), (1) and (2) are deleted and (2) is then inserted. Is this the desired behaviour? (i.e. 2 extra queries)


$col = $bracket->getTournamentLocations()->unwrap();

$col->clear();

foreach ($form->getValue('tournamentLocations') as $id) {
    $col->add($em->getReference('Tournaments*Model*TournamentLocation', $id));
}

$bracket->getTournamentLocations()->setDirty(true);```
Originally created by @doctrinebot on GitHub (Jul 21, 2011). Originally assigned to: @guilhermeblanco on GitHub. Jira issue originally created by user darkangel: Using the following code, when you go from (1, 2) to (1), (2) is deleted as expected. However, if you go from (1, 2) to (2), (1) and (2) are deleted and (2) is then inserted. Is this the desired behaviour? (i.e. 2 extra queries) `````` $bracket->getTournamentLocations()->takeSnapshot(); $col = $bracket->getTournamentLocations()->unwrap(); $col->clear(); foreach ($form->getValue('tournamentLocations') as $id) { $col->add($em->getReference('Tournaments*Model*TournamentLocation', $id)); } $bracket->getTournamentLocations()->setDirty(true);``` ``````
admin added the Improvement label 2026-01-22 13:20:01 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jul 26, 2011):

Comment created by @beberlei:

First, you are using internal API therefore you are on your own anyways.

This is marked as improvment now, the functionality works, it may just be inefficient.

@doctrinebot commented on GitHub (Jul 26, 2011): Comment created by @beberlei: First, you are using internal API therefore you are on your own anyways. This is marked as improvment now, the functionality works, it may just be inefficient.
Author
Owner

@doctrinebot commented on GitHub (Dec 9, 2011):

Comment created by @guilhermeblanco:

Hi,

I'm marking issue as invalid because you're conceptually wrong.
What you're trying to do is telling that a collection of new entities is holded by a collection of Persistent entities.
The reference internally of PersistentCollection to ArrayCollection means a lot here.

Correct code would be you to regenerate the collection (a new ArrayCollection) and just assign it to setTournamentLocations($newCollection);

Does this explanation is enough for you?

Cheers,

@doctrinebot commented on GitHub (Dec 9, 2011): Comment created by @guilhermeblanco: Hi, I'm marking issue as invalid because you're conceptually wrong. What you're trying to do is telling that a collection of new entities is holded by a collection of Persistent entities. The reference internally of PersistentCollection to ArrayCollection means a lot here. Correct code would be you to regenerate the collection (a new ArrayCollection) and just assign it to setTournamentLocations($newCollection); Does this explanation is enough for you? Cheers,
Author
Owner

@doctrinebot commented on GitHub (Dec 23, 2011):

Comment created by darkangel:

Hi Guilherme,

If I do this:

$locations = new ArrayCollection();

foreach ($form->getValue('tournamentLocations') as $id) {
    $locations->add($em->getReference('Tournaments*Model*TournamentLocation', $id));
}

$bracket->setTournamentLocations($locations);

... then all the records are deleted, before adding the new records. This is inefficient and causes extra, unnecessary write operations.

Can't Doctrine perform diffs when persisting the collection, so that only the necessary deletes and inserts are executed?

@doctrinebot commented on GitHub (Dec 23, 2011): Comment created by darkangel: Hi Guilherme, If I do this: ``` $locations = new ArrayCollection(); foreach ($form->getValue('tournamentLocations') as $id) { $locations->add($em->getReference('Tournaments*Model*TournamentLocation', $id)); } $bracket->setTournamentLocations($locations); ``` ... then all the records are deleted, before adding the new records. This is inefficient and causes extra, unnecessary write operations. Can't Doctrine perform diffs when persisting the collection, so that only the necessary deletes and inserts are executed?
Author
Owner

@doctrinebot commented on GitHub (Jan 13, 2012):

Comment created by @guilhermeblanco:

We could add it, but I don't think it worth the effort.
Main problem with this one is that we use C-level binary comparison to get the diff. That's what you entities/hash pointers are different.
We would have to write our own diff-comparator for both collections, which would probably slowdown the entire Doctrine.

I'd rather consider that it's not possible to be done at the moment, but I need much more investigation for that. This will be something that I'll probably only do when I look at this issue again with a lot of time (which is really hard to happen).

If you have some spare time, feel free to make some attempts.
Just don't forget to enable performance tests in Doctrine Unit Test suite.

@doctrinebot commented on GitHub (Jan 13, 2012): Comment created by @guilhermeblanco: We could add it, but I don't think it worth the effort. Main problem with this one is that we use C-level binary comparison to get the diff. That's what you entities/hash pointers are different. We would have to write our own diff-comparator for both collections, which would probably slowdown the entire Doctrine. I'd rather consider that it's not possible to be done at the moment, but I need much more investigation for that. This will be something that I'll probably only do when I look at this issue again with a lot of time (which is really hard to happen). If you have some spare time, feel free to make some attempts. Just don't forget to enable performance tests in Doctrine Unit Test suite.
Author
Owner

@malukenho commented on GitHub (Jan 5, 2017):

@Ocramius could it be closed as invalid?

@malukenho commented on GitHub (Jan 5, 2017): @Ocramius could it be closed as invalid?
Author
Owner

@Ocramius commented on GitHub (Jan 5, 2017):

This is a huge headache that we should work on for ORM v3 IMO. The idea that only a complete "diff" is given at the end is annoying, whereas having an add/delete would indeed optimize operations, as well as avoid duplicate index issues in join tables...

@Ocramius commented on GitHub (Jan 5, 2017): This is a huge headache that we should work on for ORM v3 IMO. The idea that only a complete "diff" is given at the end is annoying, whereas having an add/delete would indeed optimize operations, as well as avoid duplicate index issues in join tables...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1617