title: Proper way to update bidirectional Many to Many relationship symfony2-doctrine
body: <p>I did some research, and after reading <a href="https://stackoverflow.com/questions/13426653/updating-from-the-inverse-side-bidirectional-many-to-many-relationships-in-doc">this</a> and <a href="https://stackoverflow.com/questions/7044864/symfony2-doctrine-manytomany-relation-is-not-saved-to-database">this</a> (and all the related questions) I still can't figure which is the proper way to update a many to many relationship in Symonfy 2 Doctrine. It feels that there should be a very simple way of doing it that I still haven't found.</p>
  
  <p>I have this 2 entities:</p>
  
  <pre><code>class student_main
  {
  /**
  * @ORM\ManyToMany(targetEntity="support_log", inversedBy="student_main")
  * @ORM\JoinTable(name="support_log_student")
  **/
  private $support_log;
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class support_log
  {
  /**
  * @ORM\ManyToMany(targetEntity="student_main", mappedBy="support_log")
  **/
  private $student;
  </code></pre>
  
  <p>I want to start from <code>support_log</code>. In the controller, in the update action, I have something like that:</p>
  
  <pre><code>if ($editForm-&gt;isValid()) {        
    //add the relationship the user added
    foreach($students as $student){
      if(!$em-&gt;getRepository('mybundle:student_main')-&gt;hasSupportLog($entity,$student)){
          $entity-&gt;addstudent_main($student);//*
          }
      }
      $em-&gt;persist($entity);
      $em-&gt;flush();
      return $this-&gt;redirect($this-&gt;generateUrl('support_log_edit', array('id' =&gt; $id)));
  }
  </code></pre>
  
  <p>Of course, as doctrine Documentation says, I changed that function (addstudent_main) accordingly:</p>
  
  <pre><code>public function addstudent_main(student_main $student)
  {
      $student-&gt;addsupport_log($this); // the important addition
      $this-&gt;student[] = $student;
  }
  </code></pre>
  
  <p>This works fine, my question is more about deleting the relationship. In the form there is a multiselect, and the user might select some students that are already related and some that don't. It feels that there should be an automatic way of doing that, but instead I had to do lots of code.</p>
  
  <p>In the controller, just slightly above the code I wrote before, I put that:</p>
  
  <pre><code>//delete all old relationship
  foreach($idsldstudents as $idst){ //I take Id's because the doctrine collection is updating always..
              $stu=$em-&gt;getRepository('MyBundle:student_main')-&gt;find($idst);
              $stu-&gt;deletesupport_log($entity);//I had to create that method (in the entity, I do "$this-&gt;support_log-&gt;removeElement($support_log)")
              $em-&gt;persist($stu);
              $em-&gt;flush();
          }
  </code></pre>
  
  <p>I delete all the relationships of the entity in question (of course, taking in care that is a bidirectional relationship, so it has to be deleted first in the other side), and then the ones that the user selected will be added.</p>
  
  <p>There are other ways of doing that, but I haven't found any simple one. In all of them I have the same problems:</p>
  
  <ul>
  <li>I need to check all the time if the relationship exists or not</li>
  <li>I need to get the old relationships (which is difficult) and compare to the new ones that the user indicated, and delete or create accordingly</li>
  </ul>
  
  <p>Is there a way of doing that that takes care of these 2 problems automatically? (I have a strong feeling that there must be - maybe with a better declaration of the relationship? - that's why I am asking).</p>
  
  <p>Thanks in advance</p>
  
  <p>Edit:
  My form hasn't anything special, I think I didn't even touch the generated code. It displays the multiselect that I want, the default of Symfony2, where you have to use <kbd>ctrl</kbd> key to select more than one. Here is the code:</p>
  
  <pre><code>public function buildForm(FormBuilder $builder, array $options)
  {
      $builder           
          -&gt;add('student')
          ... 
          ;
  }
  </code></pre>
  
  <p>The key relies here?</p>