title: Symfony2 entity form type not saving many to many
body: <p>Problem: My many-to-many relationship is not being saved</p>
  
  <p>The form is rendered correctly with checkboxes, and if I manually add a relation between a datatypegroup and a datatype the checkbox is also checked.</p>
  
  <p>But if I check another datatype and save the form, the new relation is not established. I have tried with and without by_reference in the form, but no luck. The addDatatype method on the datatypegroup entity is not called either. Do I really have to loop the form data and find the entities myself?</p>
  
  <p>FormBuilder:</p>
  
  <pre><code>$builder
          -&gt;add('name', 'text')
          -&gt;add('datatypes', 'entity', array(
              'by_reference' =&gt; true,
              'multiple' =&gt; true,   
              'expanded' =&gt; true,   
              'class'    =&gt; 'T\DBundle\Entity\Datatype',
              'query_builder' =&gt; function(\Doctrine\ORM\EntityRepository $er) {
                      $qb = $er-&gt;createQueryBuilder('d');
                      return $qb-&gt;orderBy('d.name', 'ASC');
              }
          ));
  </code></pre>
  
  <p>DoctrineMapping:</p>
  
  <pre><code>T\DBundle\Entity\DatatypeGroup:
      type: entity
      table: DatatypeGroup
      manyToMany:
          datatypes:
              targetEntity: T\DBundle\Entity\Datatype
              mappedBy: datatypeGroups
  
  T\DBundle\Entity\Datatype:
      type: entity
      table: Datatype
  
      manyToMany:
          datatypeGroups:
              targetEntity: T\DBundle\Entity\DatatypeGroup
              inversedBy: datatypes
              joinTable:
                  name: Datatype_DatatypeGroup
                  joinColumns:
                      datatype_id:
                          referencedColumnName: id
                  inverseJoinColumns:
                      datatypeGroup_id:
                          referencedColumnName: id
  </code></pre>
  
  <p>Controller: </p>
  
  <pre><code>/**
   * @Route("/edit/{id}", name="edit")
   * @Secure("ROLE_USER")
   * @Template("TDBundle:DatatypeGroup:form.html.twig")
   */
  public function editAction(Request $request, DatatypeGroup $datatypegroup) {
      $editForm = $this-&gt;createForm(new DatatypeGroupType(), $datatypegroup);
      $editForm-&gt;handleRequest($request);
  
      if ($editForm-&gt;isValid()) {
          /** @var EntityManager $em */
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $em-&gt;flush();
  
          return $this-&gt;redirect($this-&gt;generateUrl("edit",array("id" =&gt; $datatypegroup-&gt;getId())));
      }
  
      return array(
          'datatypegroup' =&gt; $datatypegroup,
          'form'          =&gt; $editForm-&gt;createView(),
      );
  }
  </code></pre>