php - Symfony 3 orphanRemoval=true doesn't delete removed records from form -
i need t ocreate bus adding form. bus can have many amenities , that's why created onetomany relationship between bus , bus amenities.
what want achieve let doctrine remove (or update) data onetomany table without looping or doing particular in controller. read somewhere can use orphanremoval=true annotation on entity take care of reason doesn't work...
i can update existing , add new amenities (new data) can't delete records removed edit form.
can done using orphanremoval=true or know problem code:
here code:
bus entity (shorted version):
/** * @orm\onetomany(targetentity="busvehiclesamenities", mappedby="bus", cascade={"persist", "remove"}, orphanremoval=true) */ private $busvehiclesamenities; public function __construct() { $this->busvehiclesamenities = new arraycollection(); } /** * id * * @return integer */ public function getid() { return $this->id; } /** * add busvehiclesamenities * * @param \appbundle\entity\busvehiclesamenities busvehiclesamenities * @return busvehicles */ public function addbusvehiclesamenities(busvehiclesamenities $busvehiclesamenities) { /* if ($this->busvehiclesamenities->contains($busvehiclesamenities)) { return; }*/ $this->busvehiclesamenities->add($busvehiclesamenities); $busvehiclesamenities->addbus($this); return $this; } /** * remove busvehiclesamenities * * @param \appbundle\entity\busvehiclesamenities $busvehiclesamenities */ public function removebusvehiclesamenities(busvehiclesamenities $busvehiclesamenities) { $this->busvehiclesamenities->removeelement($busvehiclesamenities); $busvehiclesamenities->addbus(null); } /** * busvehiclesamenities * * @return arraycollection */ public function getbusvehiclesamenities() { return $this->busvehiclesamenities; } /** * set busvehiclesamenities * * @return arraycollection */ public function setbusvehiclesamenities($busvehiclesamenities) { $this->busvehiclesamenities = $busvehiclesamenities; }
amenities entity:
private $bus; /** * * @orm\manytoone(targetentity="amenities", inversedby="amenities") * @orm\joincolumn(name="amenities_id", referencedcolumnname="id") * */ private $amenities; /** * @var float * @orm\column(name="price", type="float", scale=2) */ protected $price; public function __construct() { } /** * id * * @return integer */ public function getid() { return $this->id; } /** * busid * * @return integer */ public function getbus() { return $this->bus; } /** * add bus * * @param \appbundle\entity\busvehicles * * @return busvehiclesamenities */ public function addbus(busvehicles $bus) { $this->bus = $bus; //$bus->setbusvehiclesamenities($this); return $this; }
controller:
public function editaction($id, request $request) { $busrepository = $this->getdoctrine()->getrepository('appbundle:busvehicles'); $bus = $busrepository->find($id); $editform = $this->createform(busvehiclestype::class, $bus); $editform->handlerequest($request); if ($editform->issubmitted() && $editform->isvalid()) { $em = $this->getdoctrine()->getmanager(); $entity = $editform->getdata(); dump ($entity); $em->persist($entity); //this should take care of saving manytomany records $em->flush(); // adding flash message our user $this->addflash('success', 'new vehicle added'); // return $this->redirecttoroute('bus_add'); } return [ 'form' => $editform->createview(), ]; }
i'm having same problem looking @ code notice you're calling
$busvehiclesamenities->addbus(null);
instead of
$busvehiclesamenities->setbus(null);
Comments
Post a Comment