Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 / Doctrine 2 : get changes to PersistentCollection

I am building an application where the user can edit some data and then gets presented with a screen where he can confirm (and comment on) his edits.

In the confirmation form I display the changes that have been made to the entity. This works for "normal" fields. Here is some code that works for checking a single field:

// create $form
// bind $form

if ($form->isValid() {
    $data = $form->getData();
    // example, get changes of a "normal" field
    if ($data['color'] != $entity->getColor()) {
        // do something with changes
    }
}

But I can't do the same for a relation (example ManyToMany with Users) :

    if ($data['users'] != $entity->getUsers()

doesn't work because $data['users'] and $entity->getUsers() refer to the same persistent collection. It is possible to call this function to see if there are changes:

    if ($data['users']->isDirty())

but it isn't possible to see what changes were made.

The second problem with the above is that if all items are removed from the persistent collection, Doctrine does not mark it as "changed" (isDirty() = true), so I can't catch the specific change where the user removes all "users" from the entity in the form.

Please note that the code all works, the only problem I have is that I am unable to view/process the changes made on the confirmation step.

like image 605
mogoman Avatar asked Feb 11 '13 12:02

mogoman


1 Answers

Doctrine\ORM\PersistentCollection has internal API (public) methods getSnapshot, getDeleteDiff, getInsertDiff that can be used during lifecycle events of the Doctrine\ORM\UnitOfWork. You could for example check the insert diff of a persistent collection during onFlush.

like image 55
Ocramius Avatar answered Sep 30 '22 20:09

Ocramius