title: Symfony 2 / Doctrine 2 : get changes to PersistentCollection
body: <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>// create $form
  // bind $form
  
  if ($form-&gt;isValid() {
      $data = $form-&gt;getData();
      // example, get changes of a "normal" field
      if ($data['color'] != $entity-&gt;getColor()) {
          // do something with changes
      }
  }
  </code></pre>
  
  <p>But I can't do the same for a relation (example ManyToMany with Users) :</p>
  
  <pre><code>    if ($data['users'] != $entity-&gt;getUsers()
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>    if ($data['users']-&gt;isDirty())
  </code></pre>
  
  <p>but it isn't possible to see what changes were made.</p>
  
  <p>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.</p>
  
  <p>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.</p>