doctrine / orm

Doctrine Object Relational Mapper (ORM)
https://www.doctrine-project.org/projects/orm.html
MIT License
9.94k stars 2.52k forks source link

Problems with tracking changes in associated relation #7365

Open MassiveHiggsField opened 6 years ago

MassiveHiggsField commented 6 years ago
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?

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?

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?

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?

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?

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.

mrtnzagustin commented 3 years ago

Same problem!! Since 2018? :(

mrtnzagustin commented 3 years ago

@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