Firstly, this question is similar to How to re-save an object as another row in Doctrine 2
The difference is that I'm trying to save data in an entity that has OneToMany relationships. I would like to re-save the object as a new line in the parent object (on the "one" side), and then as new lines in each subsequent child element (on the "many" side).
I used a fairly simple example of a class in which there were many students to make it simple.
So, I can have ClassroomA with id = 1 and it has 5 students (from 1 to 5). I would like to know how, in Doctrine2, I could take this Entity and save it in the database again (after potential data changes), all with new identifiers everywhere, and the original lines remain untouched during persist / flush.
Lets you first define our Doctrine objects.
Class object:
namespace Acme\TestBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; class Classroom { private $id; private $miscVars; protected $pupils; public function __construct() { $this->pupils = new ArrayCollection(); }
Student Institution:
namespace Acme\TestBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; class Pupil { private $id; private $moreVars; protected $classroom;
And our common Action function:
public function someAction(Request $request, $id) { $em = $this->getDoctrine()->getEntityManager(); $classroom = $em->find('AcmeTestBundle:Classroom', $id); $form = $this->createForm(new ClassroomType(), $classroom); if ('POST' === $request->getMethod()) { $form->bindRequest($request); if ($form->isValid()) {
I tried using a clone, but only kept the parent relationship (class in our example) with a fresh identifier, while the data about children (students) were updated compared to the original identifiers.
Thanks in advance for any help.
clone php symfony doctrine doctrine2
dividebyzeroZA Jan 31 '12 at 23:26 2012-01-31 23:26
source share