title: Symfony 2 save ManyToMany Unidirectional Association
body: <p>I have 3 entities:</p>
  
  <p>User (based on fosuserbundle)</p>
  
  <p>Group</p>
  
  <p>Groups (virtual to get all group)</p>
  
  <p>Role</p>
  
  <p>When I generate form everything works fine:</p>
  
  <p>GroupsType:</p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder
          -&gt;add('groups', 'collection', array('type' =&gt; new GroupType($this-&gt;ExistingRoles)))
          ;
  }
  </code></pre>
  
  <p>GroupType:</p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder
          -&gt;add('name')
          -&gt;add('groupRoles', 'entity', array(
          'class' =&gt; 'AppBundle:Role',
          'property' =&gt; 'role',
          'multiple' =&gt; true,
          'expanded' =&gt; true,
          'property' =&gt; 'name',
          'required' =&gt; false,
          'by_reference' =&gt; true,
          ))
      ;
  }
  </code></pre>
  
  <p>And I have form with All Groups and checkbox checked for each group.</p>
  
  <p>But Now I want save/update this.</p>
  
  <p>My list of manytomany group_id to role_id I defined in Group entity:</p>
  
  <pre><code>/**
   * GROUP ManyToMany with ROLE Unidirectional Association
   * @var ArrayCollection
   * @ORM\ManyToMany(targetEntity="Role",inversedBy="group")
   * @ORM\JoinTable(name="group_roles")
   */
  protected $groupRoles;
  </code></pre>
  
  <p>and in Role entity:</p>
  
  <pre><code>/**
   * @ORM\ManyToMany(targetEntity="Group", mappedBy="groupRoles")
   */
  private $group;
  </code></pre>
  
  <p>I tried something like this but doesn't work:</p>
  
  <pre><code>$all = $form-&gt;getData();
  $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
  foreach($all as $d){
      $em-&gt;getRepository('AppBundle:Group')-&gt;find($d-&gt;getId());
      $em-&gt;persist($d);
      $em-&gt;flush();
  }
  </code></pre>
  
  <p>How to save form like this?</p>