title: Symfony2: Error persisting ManyToMany/OneToMany Relationships
body: <p>I don't know why, maybe i am missing some basic logic but I always run again into the same issue. I can't persists ManyToMany collections, and it also faces me with OneToMany collections, though I can work around that.</p>
  
  <p>I read through the doctrine documentation, and I think I do understand the thing with mappedBy and inversedBy (where the last one is always the owner and therefor responsible for persisting the data, please correct me if I am wrong).</p>
  
  <p>So here's a basic example that I have right now, which I can't figure out.</p>
  
  <p>I have an Entity called <strong>Site</strong>:</p>
  
  <pre><code>#Site.php
  ...
  /**
   * @ORM\ManyToMany(targetEntity="Category", mappedBy="sites")
   */
  protected $categories;
  </code></pre>
  
  <p>and another one called <strong>Category</strong>:</p>
  
  <pre><code>#Category.php
  ...
  /**
   * @ORM\ManyToMany(targetEntity="Site", inversedBy="categories")
   * @ORM\JoinTable(name="sites_categories")
   */
  protected $sites;
  </code></pre>
  
  <p>Using the Symfony2 entity genenerator it added me some getters and setters to my Entites which look like this.</p>
  
  <p><strong>Site</strong>:</p>
  
  <pre><code>#Site.php
  ...
  /**
   * Add categories
   *
   * @param My\MyBundle\Entity\Category $categories
   */
  public function addCategory(\My\MyBundle\Entity\Category $categories)
  {
      $this-&gt;categories[] = $categories;
  }
  
  /**
   * Get categories
   *
   * @return Doctrine\Common\Collections\Collection 
   */
  public function getCategories()
  {
      return $this-&gt;categories;
  }
  </code></pre>
  
  <p>The same counts for </p>
  
  <p><strong>Category</strong>:</p>
  
  <pre><code>#Category.php
  ...
  /**
   * Add sites
   *
   * @param My\MyBundle\Entity\Site $sites
   */
  public function addSite(\My\MyBundle\Entity\Site $sites)
  {
      $this-&gt;sites[] = $sites;
  }
  
  /**
   * Get sites
   *
   * @return Doctrine\Common\Collections\Collection 
   */
  public function getSites()
  {
      return $this-&gt;sites;
  }
  </code></pre>
  
  <p>Fair enough.</p>
  
  <p>Now in my controller, I am trying to persist a Site object:</p>
  
  <pre><code>public function newsiteAction() {
  
      $site = new Site();
      $form = $this-&gt;createFormBuilder($site); // generated with the FormBuilder, so the form includes Category Entity
  
      // ... some more logic, like if(POST), bindRequest() etc.
  
          if ($form-&gt;isValid()) {
              $em = $this-&gt;getDoctrine()
              -&gt;getEntityManager();
              $em-&gt;persist($site);
              $em-&gt;flush();
          }
  }
  </code></pre>
  
  <p>The result is always the same. <strong>It persists the Site Object, but not the Category entity.</strong> And I also know why (I think): Because the Category entity is the owning side. </p>
  
  <p>But, do I always have to do something like this for persisting it? (which is actually my workaround for some OneToMany collections) </p>
  
  <pre><code>$categories = $form-&gt;get('categories')-&gt;getData();
  foreach($categories as $category) {
      // persist etc.
  }
  </code></pre>
  
  <p>But I am running into many issues here, like I would have to do the same loop as above for deleting, editing etc.</p>
  
  <p>Any hints? I will really give a cyber hug to the person who can clear my mind about that. Thanks!</p>
  
  <p>.
  .
  .</p>
  
  <h1>UPDATE</h1>
  
  <p>I ended up changing around the relationship (owning and inverse side) between the ManyToMany mapping. </p>
  
  <p>If somebody else runs into that problem, you need to be clear about the concept of bidrectional relationships, which took me a while to understand too (and I hope I got it now, see this <a href="http://doctrine-orm.readthedocs.org/en/2.0.x/reference/association-mapping.html#association-mapping-owning-inverse" rel="nofollow noreferrer">link</a>). 
  Basically what anserwed my question is: The object you want to persist <strong>must</strong> always be the owning site (The owning site is <strong>always</strong> the entity that has "inversed by" in the  annotiation).</p>
  
  <p>Also there is a concept of cascade annotation (see this <a href="http://doctrine-orm.readthedocs.org/en/2.0.x/reference/working-with-associations.html" rel="nofollow noreferrer">link</a>, thanks to <a href="https://stackoverflow.com/users/1073758/moonwave99">moonwave99</a>)</p>
  
  <p>So thanks, and I hope that helps somebody for future reference! :)</p>