title: How to update an object with ManyToMany relation with doctrine 2
body: <p>Hi there Stack Overflow members!
  I have doctrine 2 installed everything work perfectly,
  I have generated my entities and proxies with command ,</p>
  
  <p>my problem is when I try to update an entities with many to many relation ship
  I had this problem</p>
  
  <blockquote>
    <p>Fatal error: Uncaught exception 'PDOException' with message
    'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry
    '8-1' for key 'PRIMARY''</p>
  </blockquote>
  
  <p>it seems that doctrine try to insert a new entities and not trying to update joined table
  if there any problem in my code , is there any example clear ?
  thanks</p>
  
  <pre><code>    //update entities user
  
  
       $user=$this-&gt;em-&gt;getRepository('Entities\User')-&gt;find((int)$this-&gt;input-&gt;post('id'));
  
       $user-&gt;setNom($this-&gt;input-&gt;post('nom'));
       $user-&gt;setPrenom($this-&gt;input-&gt;post('prenom'));
  
      //update entities services(user have many service)
  
      foreach($this-&gt;input-&gt;post('services') as $post){
          $service = $this-&gt;em-&gt;getRepository('Entities\Service')-&gt;find((int)$post);
                if ($service instanceof Entities\Service) {
                    $user-&gt;addService($service);
          }
      $this-&gt;em-&gt;flush();
  </code></pre>