title: Symfony 2 Embedded Form Collection Many to Many
body: <p>I have 2 Entities - User and Group. They have a many-to-many relationship and Group is used to store a users' roles.</p>
  
  <p>I'm trying to make a User edit form by adding a collection, I want to be able to add a new role by selecting it from a dropdown (limited to what's already in the DB)</p>
  
  <p>UserType.php:</p>
  
  <pre><code>class UserType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('username')
              -&gt;add('email')
              -&gt;add('forename')
              -&gt;add('surname')
              -&gt;add('isActive')
              -&gt;add('joinDate', 'date', array('input' =&gt; 'datetime', 'format' =&gt; 'dd-MM-yyyy'))
              -&gt;add('lastActive', 'date', array('input' =&gt; 'datetime', 'format' =&gt; 'dd-MM-yyyy'))
              -&gt;add('groups', 'collection', array(
                      'type' =&gt; new GroupType(),
                      'allow_add' =&gt; true,
                      ))
          ;
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Sfox\CoreBundle\Entity\User'
          ));
      }
  }
  </code></pre>
  
  <p>and GroupType.php:</p>
  
  <pre><code>class GroupType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('name')
              -&gt;add('role');
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
                  "data_class" =&gt; 'Sfox\CoreBundle\Entity\Group'
                  ));
      }
  }
  </code></pre>
  
  <p>This displays the roles in the form in basic text boxes, but if I add an entry to the form, it will cascade persist a new entry into Groups and if I were to edit an entry, it would change the underlying Group data.</p>
  
  <p>I tried making a GroupSelectType.php:</p>
  
  <pre><code>class GroupSelectType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('role', 'entity', array('class'=&gt;'SfoxCoreBundle:Group', 'property'=&gt;'name'));
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
                  "data_class" =&gt; 'Sfox\CoreBundle\Entity\Group'
                  ));
      }
  }
  </code></pre>
  
  <p>Adding the field as an "entity" type, this displays the correct select box (but with the default values) I cant seem to bind it to the UserType form!</p>
  
  <p>All I want the form to do is modify the underlying 'groups' ArrayCollection in the User entity. </p>
  
  <p>Does anyone know how I can achieve this?</p>