symfony - How to remove usage of cascade={"remove","persist"} from entities in Symfony2? -
i working on project there forced use cascade={"remove","persist"} because of problem described here.
reading through documentation, quote:
even though automatic cascading convenient should used care. not blindly apply cascade=all associations unnecessarily degrade performance of application. each cascade operation gets activated doctrine applies operation association, single or collection valued.
and see same can fixed if use
$em->persist($entity);
in persistence services, calling. however, doctrine doesn't work expected. here entities.
entity/employee.php
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; /** * @orm\entity * @orm\table(uniqueconstraints={@orm\uniqueconstraint( * name="uniq_employee_id_name_address_state_city_country", * columns={"id","name","city","state","country"} * )}) */ class employee { /** * @orm\id * @orm\column(type="integer", options={"unsigned":true}) * @orm\generatedvalue(strategy="none") */ private $id; /** * @orm\column(type="string", length=255, nullable=false) */ private $name; /** * @orm\column(type="text", nullable=false) */ private $address; /** * @orm\column(type="string", length=255, nullable=false) */ private $city; /** * @orm\column(type="string", length=255, nullable=false) */ private $state; /** * @orm\column(type="string", length=255, nullable=false) */ private $country; /** * @orm\manytoone(targetentity="appbundle\entity\department", inversedby="employee", cascade={"remove","persist"}) * @orm\joincolumn(name="department_id", referencedcolumnname="id", nullable=false) */ private $department; /** * @orm\onetomany(targetentity="appbundle\entity\transfer", mappedby="employee", cascade={"remove","persist"}) */ private $transfer; } ?>
entity/department.php
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; use doctrine\common\collections\arraycollection; use doctrine\common\collections\collection; use doctrine\common\collections\selectable; /** * @orm\entity * @orm\table(uniqueconstraints={@orm\uniqueconstraint( * name="uniq_department_id_name", * columns={"id","name"} * )}) */ class department { /** * @orm\id * @orm\column(type="integer", options={"unsigned":true}) * @orm\generatedvalue(strategy="none") */ private $id; /** * @orm\column(type="string", length=255, nullable=false) */ private $name; /** * @orm\onetomany(targetentity="appbundle\entity\employee", mappedby="department", cascade={"remove","persist"}) */ private $employee; /** * @orm\onetomany(targetentity="appbundle\entity\transfer", mappedby="department", cascade={"remove","persist"}) */ private $transfer; } ?>
entity/transfer.php
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; use doctrine\common\collections\arraycollection; use doctrine\common\collections\collection; use doctrine\common\collections\selectable; /** * @orm\entity * @orm\table(uniqueconstraints={@orm\uniqueconstraint( * name="uniq_transfer_id_department_employee_start_end", * columns={"id","name"} * )}) */ class transfer { /** * @orm\id * @orm\column(type="integer", options={"unsigned":true}) * @orm\generatedvalue(strategy="auto") */ private $id; /** * @orm\column(type="date", length=255, nullable=false) */ private $start; /** * @orm\column(type="date", length=255, nullable=false) */ private $end; /** * @orm\manytoone(targetentity="appbundle\entity\employee", inversedby="attendance", cascade={"persist","remove"}) * @orm\joincolumn(name="employee_id", referencedcolumnname="id", nullable=false) */ private $employee; /** * @orm\manytoone(targetentity="appbundle\entity\department", inversedby="attendance", cascade={"persist","remove"}) * @orm\joincolumn(name="department_id", referencedcolumnname="id", nullable=false) */ private $department; } ?>
update 1:
now, have problem. since generatorvalue strategy employee , department none, have problem duplicate record error. trying use preflusheventargs remove entity before persisting if record exists in database. wonder if should complex?
thanks in advance.
it seems cascade persist operation employee department , transfer. cascade persist operation employee entity too.
it means when
$em->persist($an_employee);
you stuck in persist loop.
in opinion, cascade persist should in 1 way, i.e. on employee entity.
also if choose this, add department employee, , not opposite :
$an_employee->adddepartment($a_department); $an_employee->addtransfer($a_transfer);
that way, when persist employee, departments , transfer should persisted too
Comments
Post a Comment