title: ArrayCollection (Collection of forms) index collision in Symfony 2
body: <p>I am using Symfony2 to build up my page.
  When I try to update a collection of forms (like described in the cookbook entry "<a href="http://symfony.com/doc/current/cookbook/form/form_collections.html" rel="noreferrer">How to Embed a Collection of Forms</a>"), i get a collision of the indexes of the frontend and the indexes of the ArrayCollection in the backend.</p>
  
  <p>I've got the relation User &lt;-> Address (OneToMany). A user wants to create/update/delete his addresses, therefore he can add / delete in the frontend with the help of the javascript part new address elements. He does the following:</p>
  
  <p>(1) Adds new address (has index: 0)</p>
  
  <p>(2) Adds new address (has index: 1) and instantly removes this address again</p>
  
  <p>(3) Adds new address (has index: 2).</p>
  
  <p>When he clicks on save button, the following code saves/updates the user (and its addresses):</p>
  
  <pre><code> $this-&gt;em-&gt;persist($user);
   $this-&gt;em-&gt;flush();
  </code></pre>
  
  <p>New addresses for example are then correctly persisted to the database.
  Now the user wants to update the address e.g. with index 0.
  When he now clicks on the save button, it updates the adress with "index 0", but at the same time, it adds again the address with "index 2" to the database (object). 
  To better understand the problem, i've drawn a small illustration (handmade, sorry for my bad art skills):</p>
  
  <p><img src="https://i.stack.imgur.com/D2F69.jpg" alt="Collection error image">
  Now , i've got two times the address with "index 1" within my object / database.
  I know why this happens, it's because the first "index 1" address gets mapped to the ArrayCollection element "number 1", and the second gets mapped to "number 2 "(because of the frontend name "index 2"). 
  You can say: "it just fills up the addresses, until it reaches the frontend index in the backend"..
  But how can I fix this behaviour ?</p>
  
  <p>Site note:
  This behaviour occurs using ajax requests, because if you would reload the page after clicking "save button", it would reindex the addresses in the frontend correctly with the indexes in the backend.</p>
  
  <p><strong>My suggestion to handle that situation:</strong></p>
  
  <p>Reindexing the frontend indexes after clicking save with the server side
  indexes. Is this a clear / the only solution for my problem?</p>