title: Symfony2-Doctrine: ManyToMany relation is not saved to database
body: <p>I have two PHP model classes named Category and Item. A Category may have many Items and an Item may belong to many Categories.
  I have created a ManyToMany relation to both classes:</p>
  
  <pre><code>class Category
  {
      /**
       * @ORM\ManyToMany(targetEntity="Item", mappedBy="categories", cascade={"persist"})
       */
      private $items;
  
      /**
       * Add items
       *
       * @param Ako\StoreBundle\Entity\Item $items
       */
      public function addItems(\Ako\StoreBundle\Entity\Item $items)
      {
          $this-&gt;items[] = $items;
      }
  
      /**
       * Get items
       *
       * @return Doctrine\Common\Collections\Collection 
       */
      public function getItems()
      {
          return $this-&gt;items;
      }
  }
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>class Item
  {
      /**
       * @ORM\ManyToMany(targetEntity="Category", inversedBy="items", cascade={"persist"})
       * @ORM\JoinTable(name="item_category",
       * joinColumns={@ORM\JoinColumn(name="item_id", referencedColumnName="id")},
       * inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")}
       * )
       */
      private $categories;
  
      /**
       * Add categories
       *
       * @param Ako\StoreBundle\Entity\Category $categories
       */
      public function addCategories(\Ako\StoreBundle\Entity\Category $categories)
      {
          $this-&gt;categories[] = $categories;
      }
  
      /**
       * Get categories
       *
       * @return Doctrine\Common\Collections\Collection 
       */
      public function getCategories()
      {
          return $this-&gt;categories;
      }
  }
  </code></pre>
  
  <p>Now in my controller:</p>
  
  <pre><code>$em = $this-&gt;getDoctrine()-&gt;getEntityManager();
  
  $item = $em-&gt;getRepository('AkoStoreBundle:Item')-&gt;find($item_id);
  $category = $em-&gt;getRepository('AkoStoreBundle:Category')-&gt;find($category_id);
  
  $category-&gt;addItems($item);
  
  $em-&gt;flush();
  // Render the same page again.
  </code></pre>
  
  <p>In this page, I show the list of all items in a select field. The user can select one item, and add it to the category.</p>
  
  <p>The list of items which belong to the category are shown below the form.</p>
  
  <p>When the I submit the form, the selected item is added to the list of Category items, and is shown below, but it is not stored in the database, and if refresh the page, it disappears.</p>
  
  <p>Can anyone please help me with this?
  Thanks in advance.</p>