title: Symfony2/Doctrine: How to re-save an entity with a OneToMany as a cascading new row
body: <p>Firstly, this question is similar to <a href="https://stackoverflow.com/questions/9071094/how-to-re-save-the-entity-as-another-row-in-doctrine-2">How to re-save the entity as another row in Doctrine 2</a> </p>
  
  <p>The difference is that I'm trying to save the data within an entity that has a OneToMany relationship.  I'd like to re-save the entity as a new row in the parent entity (on the "one" side) and then as new rows in each subsequent child (on the "many" side).</p>
  
  <p>I've used a pretty simple example of a Classroom having many Pupils to keep it simple.</p>
  
  <p>So me might have ClassroomA with id=1 and it has 5 pupils (ids 1 through 5).  I'd like to know how I could, within Doctrine2, take that Entity and re-save it to the database (after potential data changes) all with new IDs throughout and the original rows being untouched during the persist/flush.</p>
  
  <p>Lets first define our Doctrine Entities.</p>
  
  <p>The Classroom Entity:</p>
  
  <pre><code>namespace Acme\TestBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * @ORM\Entity
   * @ORM\Table(name="classroom")
   */
  class Classroom
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @ORM\Column(type="string", length=255)
       */
      private $miscVars;  
  
     /**
       * @ORM\OneToMany(targetEntity="Pupil", mappedBy="classroom")
       */
      protected $pupils;
  
      public function __construct()
      {
          $this-&gt;pupils = new ArrayCollection();
      }       
      // ========== GENERATED GETTER/SETTER FUNCTIONS BELOW ============
  
  }
  </code></pre>
  
  <p>The Pupil Entity:</p>
  
  <pre><code>namespace Acme\TestBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * @ORM\Entity
   * @ORM\Table(name="pupil")
   */
  class Pupil
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @ORM\Column(type="string", length=255)
       */
      private $moreVars;
  
      /**
       * @ORM\ManyToOne(targetEntity="Classroom", inversedBy="pupils")
       * @ORM\JoinColumn(name="classroom_id", referencedColumnName="id")
       */
      protected $classroom;   
  
      // ========== GENERATED FUNCTIONS BELOW ============
  }
  </code></pre>
  
  <p>And our generic Action function:</p>
  
  <pre><code>public function someAction(Request $request, $id)
  {
      $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
  
      $classroom = $em-&gt;find('AcmeTestBundle:Classroom', $id);
  
      $form = $this-&gt;createForm(new ClassroomType(), $classroom);
  
      if ('POST' === $request-&gt;getMethod()) {
          $form-&gt;bindRequest($request);
  
          if ($form-&gt;isValid()) {
              // Normally you would do the following:
              $em-&gt;persist($classroom);
              $em-&gt;flush();
  
              // But how do I create a new row with a new ID 
              // Including new rows for the Many side of the relationship
  
              // ... other code goes here.
          }
      }
  
      return $this-&gt;render('AcmeTestBundle:Default:index.html.twig');
  }
  </code></pre>
  
  <p>I've tried using clone but that only saved the parent relationship (Classroom in our example) with a fresh ID, while the children data (Pupils) was updated against the original IDs.</p>
  
  <p>Thanks in advance to any assistance.</p>