Symfony2 How to persist ManyToMany using forms and entity type
<p>I have three tables <code>team</code>, <code>groups</code> and <code>group_team</code>. <code>group</code> has a <code>oneToMany</code> with <code>group_team</code> and <code>team</code> has a <code>OneToOne</code> with <code>group_team</code>. one group can have many teams, and one team can belong to only one group. here is my database design for it</p>

<p>how do i persist the <code>oneToMany</code> relationship of groups and team to a third table while simultaneously createing the <code>groups</code> record in the database</p>

<p><a href="https://i.stack.imgur.com/TYI3Q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TYI3Q.png" alt="enter image description here"></a></p>

<p>and here is the group entity form type</p>

<pre><code>class GroupsType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                -&gt;add('team', 'entity', array(
                                        'class' =&gt; 'CricketBundle\Model\Entity\Team',
                                        'query_builder' =&gt; function(\Doctrine\ORM\EntityRepository $er) {
                                                            return $er-&gt;createQueryBuilder('t')
                                                                      -&gt;groupBy('t.team')
                                                                      -&gt;orderBy('t.team', 'ASC');
                                                            }, 
                                        'choice_label' =&gt; 'team',
                                        'multiple'     =&gt; true,
                                        'expanded'     =&gt; true,
                                        'required' =&gt; false
                                    ))
                -&gt;add('groupname')
        ;
    }

    public function configureOptions(OptionsResolver $resolver) { 
        $resolver-&gt;setDefaults(array(
            'data_class' =&gt; 'CricketBundle\Model\Entity\Groups'
        ));
    }
}
</code></pre>

<p>here is how the groups entity form renders</p>

<p><a href="https://i.stack.imgur.com/63gIu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/63gIu.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/u0CqR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/u0CqR.png" alt="enter image description here"></a></p>