Associate entity record issue
<p>I have problem to complete ManyToMany relation in web app.</p>

<p>We have entity Contact and Tag. Relation many - to - many is written by following code:</p>

<p>Contact</p>

<pre><code>    /**
 * Contact
 *
 * @ORM\Table(name="contacts")
 * @ORM\Entity(repositoryClass="Admin\MainBundle\Repository\ContactRepository")
 */
class Contact
{

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="contacts", cascade={"all"})
 * @ORM\JoinTable(name="contact_tag_relation",
 *     joinColumns={@ORM\JoinColumn(name="contact_id", referencedColumnName="id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
 *     )
 */
private $tags;
</code></pre>

<p>Tag</p>

<pre><code>       /** 
    * Tag
    *
    * @ORM\Table(name="tags")
    * @ORM\Entity(repositoryClass="Admin\MainBundle\Repository\TagRepository")
*/
class Tag
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */

    private $id;

    /**
     * @var ArrayCollection
     * @ORM\ManyToMany(targetEntity="Admin\MainBundle\Entity\Contact", mappedBy="tags", cascade={"all"})
     * @ORM\JoinTable(name="contact_tag_relation",
     *     joinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="contact_id", referencedColumnName="id")}
     *     )
     */
    private $contacts;
</code></pre>

<p>Controller's request handler.</p>

<pre><code>    public function editAction(Request $request, Tag $tag, Contact $contact = null)
{
    $deleteForm = $this-&gt;createDeleteForm($tag);
    $editForm = $this-&gt;createForm('Admin\MainBundle\Form\TagType', $tag);
    $editForm-&gt;handleRequest($request);

    if ($editForm-&gt;isSubmitted() &amp;&amp; $editForm-&gt;isValid()) {
        $em = $this-&gt;getDoctrine()-&gt;getManager();
        $em-&gt;flush();

        return $this-&gt;redirectToRoute('tags_edit', array('id' =&gt; $tag-&gt;getId()));
    }

    return $this-&gt;render('AdminMainBundle:Tag:edit.html.twig', array(
        'tag' =&gt; $tag,
        'edit_form' =&gt; $editForm-&gt;createView(),
        'delete_form' =&gt; $deleteForm-&gt;createView(),
    ));
}
</code></pre>

<p>TagType method</p>

<pre><code>    public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        -&gt;add('title')
        -&gt;add('contacts');
}
</code></pre>

<p>After GET request to location for editing tag, server returns input field for changing tag's title filled with current value and multiselect input field with marked contacts that are currently related to the tag. 
When i make changes on multiselect field and submit, changes does not record.</p>

<p>What could be problem here? How to solve this and keep code clean?</p>

<p>The contacts attribute is represented in controller's method as persistentcollection type and its flagged as dirty.</p>

<hr>

<p>UDATE 1:</p>

<p>Tag's (ge&amp;se)tters:</p>

<pre><code>    /**
 * Add contact
 *
 * @param \Admin\MainBundle\Entity\Contact $contact
 *
 * @return Tag
 */
public function addContact(\Admin\MainBundle\Entity\Contact $contact)
{
    $this-&gt;contacts[] = $contact;

    return $this;
}

/**
 * Remove contact
 *
 * @param \Admin\MainBundle\Entity\Contact $contact
 */
public function removeContact(\Admin\MainBundle\Entity\Contact $contact)
{
    $this-&gt;contacts-&gt;removeElement($contact);
}

/**
 * Get contacts
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getContacts()
{
    return $this-&gt;contacts;
}
</code></pre>

<p>Contact's (ge&amp;se)tters:</p>

<pre><code>    /**
 * Add tag
 *
 * @param \Admin\MainBundle\Entity\Tag $tag
 *
 * @return Contact
 */
public function addTag(\Admin\MainBundle\Entity\Tag $tag)
{
    $this-&gt;tags[] = $tag;

    return $this;
}

/**
 * Remove tag
 *
 * @param \Admin\MainBundle\Entity\Tag $tag
 */
public function removeTag(\Admin\MainBundle\Entity\Tag $tag)
{
    $this-&gt;tags-&gt;removeElement($tag);
}

/**
 * Get tags
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getTags()
{
    return $this-&gt;tags;
}
</code></pre>