Symfony cloning entity persist old entity -
i've form 2 submit buttons, "save , overwrite" , "save copy".
so here problem, when i'm creating copy next code, old entity still being updated:
public function editaction($id, request $request) { $auth_checker = $this->get('security.authorization_checker'); $token = $this->get('security.token_storage')->gettoken(); // our user token $user = $token->getuser(); $em = $this->getdoctrine()->getmanager(); $checklist = $em->getrepository('appbundle:checklist')->find($id); if (!$checklist) { throw $this->createnotfoundexception('checklist not found... ' . $id); } $originalgroups = new arraycollection(); $originaltasks = new arraycollection(); $originalcorrectives = new arraycollection(); /** @var checklistgroup $group */ foreach ($checklist->getgroups() $group) { $originalgroups->add($group); /** @var checklisttask $task */ foreach ($group->gettasks() $task) { $originaltasks->add($task); foreach ($task->getcorrectives() $corrective) { $originalcorrectives->add($corrective); } } } $newtasks = new arraycollection(); $newcorrectives = new arraycollection(); $editform = $this->createform(checklisttype::class, $checklist); $editform->handlerequest($request); if ($editform->isvalid()) { foreach ($originalgroups $group) { //$checklistgroup = $em->getrepository('appbundle:checklistgroup')->find($group->getid()); if (false === $checklist->getgroups()->contains($group)) { $checklistgroup = $em->getrepository('appbundle:checklistgroup')->find($group->getid()); $em->remove($checklistgroup); } } /** @var checklistgroup $newgroup */ foreach ($checklist->getgroups() $newgroup) { /** @var checklisttask $task */ foreach ($newgroup->gettasks() $task) { $newtasks->add($task); /* if(false === $originaltasks->contains($task)) { $checklisttask = $em->getrepository('appbundle:checklisttask')->find($task->getid()); $em->remove($checklisttask); }*/ } } foreach ($originaltasks $originaltask) { if (false === $newtasks->contains($originaltask)) { $checklisttask = $em->getrepository('appbundle:checklisttask')->find($originaltask->getid()); $em->remove($checklisttask); } } /** @var checklisttask $newtask */ foreach ($checklist->getgroups() $newgroup) { /** @var checklisttask $task */ foreach ($newgroup->gettasks() $task) { foreach ($task->getcorrectives() $lcorrective) { $newcorrectives->add($lcorrective); } } } foreach ($originalcorrectives $originalcorrective) { if (false === $newcorrectives->contains($originalcorrective)) { $checklistcorrective = $em->getrepository('appbundle:checklisttaskcorrective')->find($originalcorrective->getid()); $em->remove($checklistcorrective); } } $checklist->setusermodificator($user); if ($editform->get('save')->isclicked()) { $em->persist($checklist); $em->flush(); } if ($editform->get('savecopy')->isclicked()) { $copy = clone $checklist; $copy->setnombre($checklist->getnombre(). ' #copy#'); $em->persist($copy); $em->refresh($checklist); $em->flush(); } $this->get('session')->getflashbag()->add('success', "se ha actualizado el checklist correctamente."); return $this->redirecttoroute('listchecklist', array('id' => $id)); } return $this->render(':checklist:create.html.twig', array( 'edit' => true, 'form' => $editform->createview(), )); }
even after detached , explicitly call flush($copy).
to handle deep copy have next codes:
checklist
public function __clone() { if ($this->id) { $this->setid(null); $groupsclone = new arraycollection(); /** @var checklistgroup $group */ foreach ($this->groups $group) { $itemclone = clone $group; $itemclone->setchecklist($this); $groupsclone->add($itemclone); } $this->groups = $groupsclone; } }
checklistgroup
public function __clone() { if ($this->id) { $this->id = null; // cloning relation m onetomany $tasksclone = new arraycollection(); /** @var checklisttask $task */ foreach ($this->gettasks() $task) { $itemclone = clone $task; $itemclone->setchecklistgroup($this); $tasksclone->add($itemclone); } $this->tasks = $tasksclone; } }
checklisttask
public function __clone() { if ($this->id) { $this->id = null; // cloning relation m onetomany $correctivesclone = new arraycollection(); /** @var checklisttaskcorrective $corrective */ foreach ($this->getcorrectives() $corrective) { $itemclone = clone $corrective; $itemclone->setchecklisttask($this); $correctivesclone->add($itemclone); } $this->correctives = $correctivesclone; } }
checklistcorrective
public function __clone() { if($this->id) { $this->setid(null); } }
the result both copy , original checklist stored changes, example if add 1 group , 2 tasks, 2 checklists entities groups , task in both of them.
also in relations i've persist={"cascade"}, don't know if may problem.
what's wrong?
edit
this after testing each entity state:
group 001 managed task 001 managed accion correctora 1 managed task 002 managed task 003 managed group 002 managed task 001 managed accion correctora test managed task 002 managed group 003 managed task 001 managed task 002 managed task 003 managed xzcxzc new xzcxzc new xzcxzc new
existing entities of groups, tasks , correctives appear managed, added ones appear new, $old_checklist appear detached , $copy appear managed.
Comments
Post a Comment