title: Symfony2 and Doctrine: OneToMany relation duplicates data in the table
body: <p>I have two tables: articles and emails.
  The emails table will contain emails related to the specific articles, for example:</p>
  
  <pre><code>id    |  article_id |     email
  1     |      1      |  test@etest.com
  2     |      1      |  test2@test.com
  3     |      2      |  test@etest.com
  4     |      2      |  test3@test.com
  </code></pre>
  
  <p>Etc....</p>
  
  <p>There is a relation between article_id and the id from the articles table.</p>
  
  <p>In my entity, I have this code:</p>
  
  <pre><code>class Articles
  { 
      ....
       /**
        * @ORM\OneToMany(targetEntity="\Acme\DemoBundle\Entity\Emails", mappedBy="articles")
        */
        private $emails_rel; 
  
      ...
  }
  
  class Emails
  { 
      /**
       * @ORM\ManyToOne(targetEntity="\Acme\DemoBundle\Entity\Articles", inversedBy="emails_rel", cascade={"all"})
       * @ORM\JoinColumn(name="article_id", referencedColumnName="id")
       **/
      private $articles; 
  }
  </code></pre>
  
  <p>In my controller, i am doing some tests where I will persist or not some entities.
  At  the end, I am doing a flush</p>
  
  <pre><code>$em-&gt;flush(); 
  </code></pre>
  
  <p>And the strange behaviour is that in my Emails table, the data is duplicated as soon as I am doing the flush. When testing the entity manager with</p>
  
  <pre><code>$em-&gt;getUnitOfWork()-&gt;getScheduledEntityInsertions()
  </code></pre>
  
  <p>I am getting an empty array.</p>
  
  <p>Any idea why? Thank you very much.</p>
  
  <p>EDIT:</p>
  
  <p>Here is the test:</p>
  
  <pre><code>$articl = new Articles();
  $articl = $em-&gt;createQuery("SELECT p FROM Acme\DemoBundle\Entity\Articles p WHERE p.bMailToMatch = 1")-&gt;getResult();
  $nb = count($articl);
  
  // BECAUSE I WILL WORK WITH A LOTS OF ENTRIES - PREVENT MEMORY OVERFLOW
  $em-&gt;clear();    
  
  if(isset( $articl )) {
      foreach($articl as $i =&gt; $art) {
          $array_emails = null;
  
          $art_emails = $art-&gt;getEmailsRel();
          foreach ($art_emails as $e) {
              $array_emails[] = $e-&gt;getEmail();
          }
  
          $art_id = $art-&gt;getId ();
          echo "\n\n---------- $i ----------\n " . $art-&gt;getDoi ();
  
          // TAKES ARTICLE WITH ZERO EMAIL
          if (!isset($array_emails) ) {
              $updated_article = $em-&gt;getRepository('AcmeDemoBundle:Articles')-&gt;findOneBy(array( 'id' =&gt; ($art_id)    )) ; 
              // Because of the clearing of the entity manager
              echo"\n\n$art_id Article DOI \n".$updated_article-&gt;getDoi();
              echo "\n==&gt;Put the BMailToMatch's flag to 0";
              $updated_article-&gt;setBMailToMatch(0);
              $em-&gt;persist($updated_article);
          }
          else {
              echo " ==&gt; ok\n";
          }
  
          if (($i % 3) == 0) {
              $em-&gt;flush();
              $em-&gt;clear();
          }
      }
  
      $em-&gt;flush();
      $em-&gt;clear();
  }
  
  return new Response ( "\n\nFinished!!\n" );             
  </code></pre>