title: Duplicate entry with form choices
body: <p>I have a classified with categories. My classified can have one or several categories:</p>
  
  <pre><code>/**
   * @ORM\OneToMany(targetEntity="Category", mappedBy="classified", cascade={"persist"}, orphanRemoval=true)
   */
  protected $categories;
  </code></pre>
  
  <p>An important thing to know is that my <strong>Category entity has composite primary keys</strong> : the ID of the category, and the classified.</p>
  
  <p>I want to populate categories into my classified with a choices field:</p>
  
  <pre><code>$builder-&gt;add(
      $builder-&gt;create('categories', ChoiceType::class, [
          'choices' =&gt; [
              'Category 1' =&gt; 1,
              'Category 2' =&gt; 2,
              'Category 3' =&gt; 3,
              'Category 4' =&gt; 4,
          ],
          'multiple' =&gt; true,
      ])-&gt;addModelTransformer(new CallbackTransformer(
          // Transform collection of entities into an array of IDs
          function($categories) {
              if (!$categories) {
                  return [];
              }
  
              return $categories-&gt;map(function(Category $category) {
                  return $category-&gt;getId();
              })-&gt;toArray();
          },
          // Reverse transform IDs array from request to a collection of entities
          function($categories) {
              $items = [];
              if ($categories &amp;&amp; is_array($categories)) {
                  foreach ($categories as $id) {
                      $items[] = new Category($id);
                  }
              }
  
              return $items;
          }
      ))
  );
  </code></pre>
  
  <p>As you can see I have a transformer to convert the categories collection to a list of ID, and vice versa.</p>
  
  <p>Everything is fine during the creation of my classified, but <strong>edition is bugged</strong>.</p>
  
  <p>My problem is that when I submit the form, I have duplicate entries issues:</p>
  
  <pre><code>An exception occurred while executing 'INSERT INTO tv_classified_category (id, classified_id) VALUES (?, ?)' with params [4, 1]:
  
  SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '4-1' for key 'PRIMARY'
  </code></pre>
  
  <p>Here is what I found when I dump the list of categories before and after <code>handleRequest</code>:</p>
  
  <pre><code>    dump($classified-&gt;getCategories()-&gt;toArray());
      $form-&gt;handleRequest($request);
      if ($form-&gt;isValid()) {
          dump($classified-&gt;getCategories()-&gt;toArray());
  </code></pre>
  
  <p>Debug result:</p>
  
  <pre><code>array:2 [▼
    0 =&gt; Category {#1036 ▶}
    1 =&gt; Category {#1038 ▶}
  ]
  
  array:3 [▼
    2 =&gt; Category {#4108 ▶}
    3 =&gt; Category {#4110 ▶}
  ]
  </code></pre>
  
  <p>As you can see, there is a strange <strong>mismatch between keys</strong>, and the PHP i<strong>ds of objects are differents</strong> before and after the handleRequest call. What is wrong and how can I fix this?</p>
  
  <p>Regards,</p>