php - Symfony Doctrine: Remove all ManyToMany relations -
i have manytomany relationship between 2 entities (clientproject & tour) working except 1 case. when want clear relations between entities, doesn't work , database not updated accordingly.
clientproject.php
/** * @orm\manytomany(targetentity="tour", inversedby="clientprojects") * @orm\jointable( * name="client_projects_tour_items", * joincolumns={ * @orm\joincolumn(name="client_project_id", referencedcolumnname="id") * }, * inversejoincolumns={ * @orm\joincolumn(name="tour_id", referencedcolumnname="id") * } * ) */ protected $touritems; public function removetouritem(\sharedbundle\entity\tour $touritem) { $this->touritems->removeelement($touritem); }
tour.php
public function removeclientproject(\sharedbundle\entity\clientproject $clientproject) { $this->clientprojects->removeelement($clientproject); }
working example:
- before update:
- tour 1 / clientproject 3
- tour 1 / clientproject 4
- action: remove relation between tour 1 , clientproject 4
- after update:
- tour 1 / clientproject 3
failing example:
- before update:
- tour 1 / clientproject 3
- action: remove relation between tour 1 , clientproject 3
- after update:
- tour 1 / clientproject 3
controller action
public function editclientprojectaction($id, request $request) { if (!$request->isxmlhttprequest()) { return $this->redirecttoroute('contact'); } $clientproject = $this->getdoctrine()->getrepository(clientproject::class)->find($id); if(!$clientproject) { return new jsonresponse(array('redirect' => $this->generateurl('contacts'))); } $form = $this->createform(clientprojectformtype::class, $clientproject, array( 'action' => $this->generateurl('edit-client-project', array('id' => $id)) )); $form->setdata($clientproject); if ($request->ismethod('post')) { $form->submit($request->request->get($form->getname()), false); if($form->issubmitted() && $form->isvalid()) { $em = $this->getdoctrine()->getmanager(); $em->persist($clientproject); $em->flush(); return new jsonresponse(array('reloaddatatable' => 'client_project_datatable')); } else { return new jsonresponse(array('reload' => true)); } } return $this->render('@appbundle/client-projects/edit-client-project.html.twig', [ 'form' => $form->createview(), ]); }
in order make remove work, think need add code remove methods:
# entity/clientproject.php /** * remove touritem * * @param \sharedbundle\entity\tour $touritem */ public function removetouritem(\sharedbundle\entity\tour $touritem) { if (!$this->touritems->contains($touritem)) { return; } $this->touritems->removeelement($touritem); $clientproject->removetouritem($this); } # entity/tour.php /** * remove clientproject * * @param \sharedbundle\entity\clientproject $clientproject */ public function removeclientproject(\sharedbundle\entity\clientproject $clientproject) { if (!$this->clientproject->contains($clientproject)) { return; } $this->clientprojects->removeelement($clientproject); $touritem->removeclientproject($this); }
also, in tour.php
file need add annotation:
cascade={"persist", "remove"}
when set relation.
Comments
Post a Comment