Problems with tracking changes in associated relation #6044

Open
opened 2026-01-22 15:25:24 +01:00 by admin · 3 comments
Owner

Originally created by @MassiveHiggsField on GitHub (Aug 22, 2018).

Q A
Version v2.6.2

Support Question

So, i've been trying to track changes to an associated relation in an onflush listener.

I'm using this code to check for changes:

foreach ($unitOfWork->getScheduledCollectionDeletions() as $delete) {
    /** @var PersistentCollection $delete */
    $insertDiffDeletions = $delete->getInsertDiff();
    $deleteDiffDeletions = $delete->getDeleteDiff();
    ...
}
foreach ($unitOfWork->getScheduledCollectionUpdates() as $update) {
    /** @var PersistentCollection $update */
    $insertDiffUpdates = $update->getInsertDiff();
    $deleteDiffUpdates = $update->getDeleteDiff();
    ...
}

But the results seem to be inconsistent or not usable. Here are my findings:

Scenario: My associated list has three items in it

Case 1: What happens if i remove one item from the association?

  • getScheduledCollectionDeletions is empty
  • getScheduledCollectionUpdates has one entry
    • $update->getInsertDiff() is empty
    • $update->getDeleteDiff() has one entry

This seems a litte bit weird, i would expect getScheduledCollectionDeletions would have the entry, but fine, i can work with this (i've also tried removing two items, which also worked and getDeleteDiff had two entries).

Case 2: What happens if i add one item to the association?

  • getScheduledCollectionDeletions is empty
  • getScheduledCollectionUpdates has one entry
    • $update->getInsertDiff() has one entry
    • $update->getDeleteDiff() is empty

This would be as expected (i've also tried adding two items, which also worked and getInsertDiff had two entries)

Case 3: What happens if i add one item and remove one item from the association?

  • getScheduledCollectionDeletions is empty
  • getScheduledCollectionUpdates is empty

This cannot be right. There seems to be no way to find out that this list has been changed.

Case 4: What happens if i add two items and remove one item from the association?

  • getScheduledCollectionDeletions is empty
  • getScheduledCollectionUpdates has one entry
    • $update->getInsertDiff() has one entry
    • $update->getDeleteDiff() is empty

Once again, this cant be right. Only one addition is tracked.

Case 5: What happens if i add one item and remove two items from the association?

  • getScheduledCollectionDeletions is empty
  • getScheduledCollectionUpdates has one entry
    • $update->getInsertDiff() is empty
    • $update->getDeleteDiff() has one entry

Once again, doesnt seem to be right. Only one deletion is tracked.

So, i cant figure out how to track all changes to the associated list. Any help appreciated? Maybe this is even a bug. It seems to me the getScheduledCollectionUpdates calculates changes by comparing the list length before and after, not really comparing the changes to the list.

Originally created by @MassiveHiggsField on GitHub (Aug 22, 2018). | Q | A |------------ | ----- | Version | v2.6.2 ### Support Question So, i've been trying to track changes to an associated relation in an onflush listener. I'm using this code to check for changes: foreach ($unitOfWork->getScheduledCollectionDeletions() as $delete) { /** @var PersistentCollection $delete */ $insertDiffDeletions = $delete->getInsertDiff(); $deleteDiffDeletions = $delete->getDeleteDiff(); ... } foreach ($unitOfWork->getScheduledCollectionUpdates() as $update) { /** @var PersistentCollection $update */ $insertDiffUpdates = $update->getInsertDiff(); $deleteDiffUpdates = $update->getDeleteDiff(); ... } But the results seem to be inconsistent or not usable. Here are my findings: **Scenario: My associated list has three items in it** **Case 1**: What happens if i remove one item from the association? - getScheduledCollectionDeletions is empty - getScheduledCollectionUpdates has one entry - $update->getInsertDiff() is empty - $update->getDeleteDiff() has one entry This seems a litte bit weird, i would expect getScheduledCollectionDeletions would have the entry, but fine, i can work with this (i've also tried removing two items, which also worked and getDeleteDiff had two entries). **Case 2**: What happens if i add one item to the association? - getScheduledCollectionDeletions is empty - getScheduledCollectionUpdates has one entry - $update->getInsertDiff() has one entry - $update->getDeleteDiff() is empty This would be as expected (i've also tried adding two items, which also worked and getInsertDiff had two entries) **Case 3**: What happens if i add one item and remove one item from the association? - getScheduledCollectionDeletions is empty - getScheduledCollectionUpdates is empty This cannot be right. There seems to be no way to find out that this list has been changed. **Case 4**: What happens if i add two items and remove one item from the association? - getScheduledCollectionDeletions is empty - getScheduledCollectionUpdates has one entry - $update->getInsertDiff() has one entry - $update->getDeleteDiff() is empty Once again, this cant be right. Only one addition is tracked. **Case 5**: What happens if i add one item and remove two items from the association? - getScheduledCollectionDeletions is empty - getScheduledCollectionUpdates has one entry - $update->getInsertDiff() is empty - $update->getDeleteDiff() has one entry Once again, doesnt seem to be right. Only one deletion is tracked. So, i cant figure out how to track all changes to the associated list. Any help appreciated? Maybe this is even a bug. It seems to me the getScheduledCollectionUpdates calculates changes by comparing the list length before and after, not really comparing the changes to the list.
Author
Owner

@mrtnzagustin commented on GitHub (Oct 6, 2021):

Same problem!! Since 2018? :(

@mrtnzagustin commented on GitHub (Oct 6, 2021): Same problem!! Since 2018? :(
Author
Owner

@mrtnzagustin commented on GitHub (Oct 6, 2021):

@DamienHarper are you aware of this? i was checking your code at 6843e88551/src/Provider/Doctrine/Auditing/Transaction/TransactionHydrator.php to create a logger for a legacy migration with tables that have composite keys everywhere

@mrtnzagustin commented on GitHub (Oct 6, 2021): @DamienHarper are you aware of this? i was checking your code at https://github.com/DamienHarper/auditor/blob/6843e885518a1d8b61868fc0ba36a257a28ad828/src/Provider/Doctrine/Auditing/Transaction/TransactionHydrator.php to create a logger for a legacy migration with tables that have composite keys everywhere
Author
Owner

@mmarton commented on GitHub (May 7, 2025):

Still an issue in 2025, since 2013
https://stackoverflow.com/questions/15311083/whats-the-proper-use-of-unitofwork-getscheduledcollectiondeletions-in-doctr

@mmarton commented on GitHub (May 7, 2025): Still an issue in 2025, since 2013 https://stackoverflow.com/questions/15311083/whats-the-proper-use-of-unitofwork-getscheduledcollectiondeletions-in-doctr
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6044