title: Symfony2 Doctrine ManyToMany doesn't persist
body: <p>Hey guys I'm looking for help again.</p>
  
  <p>My problem is that two related ORM entities not getting stored.</p>
  
  <p>I run the following Unit test and it fails:</p>
  
  <pre><code>public function testCategoryRelation()
  {
      $doctrine = $this-&gt;client-&gt;getContainer()-&gt;get('doctrine');
      $itemRepo = $doctrine-&gt;getRepository("AppBundle:Item");
      $item = $itemRepo-&gt;find(230045);
      $this-&gt;assertTrue($item instanceof \Acme\TestBundle\Entity\Item);
  
      $categories = $item-&gt;getCategories();
      $this-&gt;assertGreaterThan(0, $categories-&gt;count());
      foreach ($categories as $category) {
          $this-&gt;assertTrue($category instanceof \Acme\TestBundle\Entity\Category);
      }
  
      $count = $categories-&gt;count();
      $categoryRepo = $doctrine-&gt;getRepository("AppBundle:Category");
      $categories = $categoryRepo-&gt;findBy(array(), null, 2);
      $this-&gt;assertEquals( 2, count($categories) );
  
      foreach ($categories as $category) {
          $this-&gt;assertTrue($category instanceof \Acme\TestBundle\Entity\Category);
          $category-&gt;addItem($item);
          $item-&gt;addCategory($category);
      }
      $this-&gt;assertGreaterThan($count, $item-&gt;getCategories()-&gt;count());
  
      $em = $doctrine-&gt;getManager();
      foreach ($categories as $category) {
  
          $em-&gt;persist($category);
      }
      $em-&gt;persist($item);
      $em-&gt;flush();
      $em-&gt;clear($item);
  
      $item = $itemRepo-&gt;find(230045);
      $categories = $item-&gt;getRelatedItems();
      $this-&gt;assertGreaterThan($count, $categories-&gt;count()); /* Here it fails */
  }
  </code></pre>
  
  <p>I have no clue why the last test fails. Could you please help me find my mistake?</p>
  
  <p>I run Symfony 2.1.4 together with Doctrine 2.3</p>
  
  <p>I have two ORM Objects.</p>
  
  <h2>1. Item</h2>
  
  <pre><code>namespace Acme\TestBundle\Entity;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * @ORM\Table(name="item")
   */
  class Item
  {
  
    /**
     * @ORM\ManyToMany(targetEntity="Category", mappedBy="items")
     */
    private $categories;
  
    public function __construct()
    {
        $this-&gt;categories = new \Doctrine\Common\Collections\ArrayCollection();
    }
  
    /**
     * @param \Acme\TestBundle\Entity\Category $category
     */
    public function addCategory(\Acme\TestBundle\Entity\Category $category)
    {
        $this-&gt;categories[] = $category;
        return $this;
    }
  
    /**
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getCategories()
    {
        return $this-&gt;categories;
    }
  }
  </code></pre>
  
  <h2>2. Category</h2>
  
  <pre><code>namespace Acme\TestBundle\Entity;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * @ORM\Table(name="category")
   * @ORM\Entity(repositoryClass="Acme\TestBundle\Repository\CategoryRepository")
   */
  class Category
  {
    /**
     * @var \Doctrine\Common\Collections\ArrayCollection
     *
     * @ORM\ManyToMany(targetEntity="Item", inversedBy="categories")
     * @ORM\JoinTable(name="category_item",
     *   joinColumns={
     *     @ORM\JoinColumn(name="cat_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="item_id", referencedColumnName="id")
     *   }
     * )
     */
    private $items;
  
    /**
     * @param \Acme\TestBundle\Entity\Item $item
     * @return Category
     */
    public function addItem(\Acme\TestBundle\Entity\Item $item)
    {
        $this-&gt;items[] = $item;
        return $this;
    }
  
    /**
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getItems()
    {
        return $this-&gt;items;
    }
  }
  </code></pre>
  
  <p>Thank you in advance for your help!</p>