title: How to work correctly with Forms, FOS Rest Bundle and many to many relations in Symfony2
body: <p>I'm working with Symfony2 Forms and FOSRestBundle.</p>
  
  <p>I'm trying to save in the database, an entity with a many to many relationship.</p>
  
  <p>I create a Form with a collection field (<a href="http://symfony.com/doc/master/cookbook/form/form_collections.html" rel="noreferrer">http://symfony.com/doc/master/cookbook/form/form_collections.html</a>) like this:</p>
  
  <pre><code>class MainType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('name');
          $builder-&gt;add('description');
  
          $builder-&gt;add('others', 'collection', array(
              'type' =&gt; new OtherType()
          ));
      }
  
      public function getName()
      {
          return '';
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Acme\SearchBundle\Entity\Main',
              'csrf_protection' =&gt; false
          ));
      }
  }
  
  class OtherType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('id');
      }
  
      public function getName()
      {
          return '';
      }
  
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Acme\SearchBundle\Entity\Other',
              'csrf_protection' =&gt; false
          ));
      }
  }
  </code></pre>
  
  <p>The collection of objects of type "Other" is stored in the database. And I don't want to store more objects of that type, only read and relate them to the main object.</p>
  
  <p>When I process the form I use this function:</p>
  
  <pre><code>private function processForm(Main $main, $new = false)
  {
  
      $new = true;
  
      $statusCode = $new ? 201 : 204;
  
      $form = $this-&gt;createForm(new MainType(), $main);
      $form-&gt;bind($this-&gt;getRequest());
  
  
      if ($form-&gt;isValid()) {
  
          $mainValidated = $form-&gt;getData();
  
          // I should store the collection of objects of type other
          // in the database
  
          $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
          $em-&gt;persist($mainValidated);
          $em-&gt;flush();
  
          return $this-&gt;view($new ? $mainValidated : null, $statusCode);
      }
  
      return $this-&gt;view($form, 400);
  }
  </code></pre>
  
  <p>The code json I send from a client Backbone.js is:</p>
  
  <pre><code>{"others":[{"id":1}, {"id":2}]}
  </code></pre>
  
  <p>Entities:</p>
  
  <ul>
  <li>Main</li>
  </ul>
  
  <p>Xml:</p>
  
  <pre><code>  &lt;entity name="Acme\SearchBundle\Entity\Main" table="main"&gt;
      &lt;id name="id type="integer" column="id"&gt;
        &lt;generator strategy="IDENTITY"/&gt;
      &lt;/id&gt;
      &lt;field name="name" type="integer" column="name" nullable="true"/&gt;
      &lt;field name="description" type="integer" column="description" nullable="true"/&gt;
  
      &lt;many-to-many field="others" target-entity="Other" inversed-by="mains"&gt;
        &lt;cascade&gt;
           &lt;cascade-persist/&gt;
        &lt;/cascade&gt;
        &lt;join-table name="main_has_other"&gt;
          &lt;join-columns&gt;
            &lt;join-column name="main" referenced-column-name="id"/&gt;
          &lt;/join-columns&gt;
          &lt;inverse-join-columns&gt;
            &lt;join-column name="other" referenced-column-name="id"/&gt;
          &lt;/inverse-join-columns&gt;
        &lt;/join-table&gt;
      &lt;/many-to-many&gt;
  
    &lt;/entity&gt;
  </code></pre>
  
  <p>Entity:</p>
  
  <pre><code>&lt;?php
  
  namespace Acme\SearchBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  use JMS\Serializer\Annotation\Type;
  
  use JMS\Serializer\Annotation\Groups;
  use JMS\Serializer\Annotation\Expose;
  
  class Main
  {   
      /**
       * @Type("integer")
       * @Groups({"admin"})
       * 
       * @var integer
       * 
       */
      private $id;
  
      /**
       * 
       * @Type("string")
       * @Groups({"manage"})
       * 
       * @var string
       */
      private $name;
  
  
      /**
       * @Type("string")
       * @Groups({"manage"})
       * 
       * @var string
       */
      private $description;
  
  
      /**
       * @Type("ArrayCollection&lt;Acme\SearchBundle\Entity\Other&gt;")
       * @Groups({"manage"})
       * 
       * @var \Doctrine\Common\Collections\Collection
       */
      private $others;
  
  
      /**
       * Constructor
       */
      public function __construct()
      {
          $this-&gt;others = new \Doctrine\Common\Collections\ArrayCollection();
      }
  
  
  
      /**
       * Set name
       *
       * @param string $name
       * @return Main
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set description
       *
       * @param string $description
       * @return Main
       */
      public function setDescription($description)
      {
          $this-&gt;description = $description;
  
          return $this;
      }
  
      /**
       * Get description
       *
       * @return string
       */
      public function getDescription()
      {
          return $this-&gt;description;
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Add others
       *
       * @param \Acme\SearchBundle\Entity\Other $other
       * @return Main
       */
      public function addOthers(\Acme\SearchBundle\Entity\Other $other)
      {
          $this-&gt;others[] = $other;
  
          return $this;
      }
  
      /**
       * Remove others
       *
       * @param \Acme\SearchBundle\Entity\Other $other
       */
      public function removeOthers(\Acme\SearchBundle\Entity\Other $other)
      {
          $this-&gt;others-&gt;removeElement($other);
      }
  
      /**
       * Get others
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getOthers()
      {
          return $this-&gt;others;
      }
  }
  </code></pre>
  
  <ul>
  <li>Other</li>
  </ul>
  
  <p>Xml:</p>
  
  <pre><code>&lt;entity name="Acme\SearchBundle\Entity\Other" table="other"&gt;
    &lt;id name="id" type="integer" column="id"&gt;
      &lt;generator strategy="IDENTITY"/&gt;
    &lt;/id&gt;
    &lt;field name="name" type="string" column="name" length="255" nullable="true"/&gt;
    &lt;field name="description" type="string" column="name" length="255" nullable="true"/&gt;
    &lt;many-to-many field="mains" target-entity="Main" mapped-by="others"&gt;
      &lt;cascade&gt;
          &lt;cascade-persist/&gt;
      &lt;/cascade&gt;
    &lt;/many-to-many&gt;
  &lt;/entity&gt;
  </code></pre>
  
  <p>Entity:</p>
  
  <pre><code>&lt;?php
  
  namespace Acme\SearchBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  use JMS\Serializer\Annotation\Type;
  use JMS\Serializer\Annotation\Groups;
  
  class Other
  {
  
     /**
       * @Type("integer")
       * @Groups({"manage"})
       * 
       * @var integer
       */
      private $id;
  
      /**
       * @Type("string")
       * @Groups({"manage"})
       * 
       * @var string
       */
      private $name;
  
      /**
       * @Type("string")
       * @Groups({"manage"})
       * 
       * @var string
       */
      private $description;
  
      /**
       * @Type("Acme\SearchBundle\Entity\Main")
       * @Groups({"admin"})
       * 
       * @var \Doctrine\Common\Collections\Collection
       */
      private $mains;
  
      /**
       * Constructor
       */
      public function __construct()
      {
          $this-&gt;mains = new \Doctrine\Common\Collections\ArrayCollection();
      }        
  
      /**
       * Set name
       *
       * @param string $name
       * @return Other
       */
      public function setName($name)
      {
          $this-&gt;name = $name
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set description
       *
       * @param string $description
       * @return Other
       */
      public function setDescription($description)
      {
          $this-&gt;description = $description;
  
          return $this;
      }
  
      /**
       * Get description
       *
       * @return string 
       */
      public function getDescription()
      {
          return $this-&gt;description;
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set id
       *
       * @param integer $id
       * @return Other
       */
      public function setId($id)
      {
          $this-&gt;id = $id;
  
          return $this;
      }
  
      /**
       * Add main
       *
       * @param \Acme\SearchBundle\Entity\Main $main
       * @return Other
       */
      public function addMains(\Acme\SearchBundle\Entity\Main $main)
      {
          $this-&gt;mains[] = $main;
  
          return $this;
      }
  
      /**
       * Remove main
       *
       * @param \Acme\SearchBundle\Entity\Main $main
       */
      public function removeMains(\AcmeSearchBundle\Entity\Main $main)
      {
          $this-&gt;mains-&gt;removeElement($main);
      }
  
      /**
       * Get mains
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getMains()
      {
          return $this-&gt;mains;
      }
  }
  </code></pre>
  
  <p>When I persist the object of type "main" in the database, the collection is not persited in the table of many to many relationship. I have to save the collection manually when persist the "main" object. </p>
  
  <p>I'm looking for a way to save the collection of objects automatically as easy as possible.</p>