title: Edit many-to-many relations in Symfony2
body: <p>I am using Symfony2 with Doctrine and want to edit the relation between two tables represented with an intermediate table. The structure is like this: </p>
  
  <p>Entity Tour stored in table tour</p>
  
  <p>Entity Tag stored in table tags</p>
  
  <p>I have a middle table tour_tags to store the two indexes per row: tour_id, tag_id.</p>
  
  <p>And the entity definition is as follows:</p>
  
  <pre><code>/**
   * @ORM\Table(name="tour")
   * @ORM\Entity
   */
  Tour {
    /**
     * @ORM\ManyToMany(targetEntity="Tag")
     * @ORM\JoinTable(name="tour_tags",
     *     joinColumns={@ORM\JoinColumn(name="tour_id", referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
     *     )
     */
    private $tags;
  
    ...
  }
  
  /**
   * @ORM\Table(name="tags")
   * @ORM\Entity
   */
  class Tag {
    ...
  }
  </code></pre>
  
  <p>As you can see I didn't put any relation info in the Tag entity.
  This schema is working ok in obtaining the related data, tags for each tour and so.</p>
  
  <p>But I want the user to be able to edit the relation info via checkboxes in the Tour form. I want to include a set of checkboxes, one for existing Tag in the table.
  With this doc <a href="http://symfony.com/doc/current/cookbook/form/form_collections.html" rel="nofollow">http://symfony.com/doc/current/cookbook/form/form_collections.html</a> it's possible to edit the tag names from the tour form: </p>
  
  <pre><code>/* The tour form */
  Class TourType {
    public function buildForm(FormBuilder $builder, array $options) {
      ...
      $builder-&gt;add('tags', 'collection', array('type' =&gt; new TourTagsType()));
    }
  }
  </code></pre>
  
  <p>but not create or delete relations (table tour_tags).</p>
  
  <p>I am looking for a way to embeed the relation (tour_tag) in the form so the checkboxes appear checked if there's a tour_id, tag_id row in the table.</p>