我正在开展一个项目,因为problem described here.
我被迫使用cascade = {“remove”,“persist”}阅读documentation,引用:
即使自动级联很方便,也应小心使用。不要盲目地将cascade = all应用于所有关联,因为它会不必要地降低应用程序的性能。对于每个被激活的级联操作,Doctrine也会将该操作应用于关联,无论是单个还是集合值。
如果我使用
,我发现同样可以修复$em->persist($entity);
在我已经调用的持久性服务中。但是,学说并不像预期的那样有效。这是我的实体。
实体/ Employee.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping AS 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;
}
?>
实体/ Department.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping AS 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;
}
?>
实体/ Transfer.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping AS 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;
}
?>
更新1:
现在,我有另一个问题。由于我的Employee和Department的GeneratorValue策略是NONE,我遇到重复记录错误的问题。我试图使用PreFlushEventArgs删除实体,如果该记录存在于数据库中则持久化。但我想知道它应该是那么复杂吗?
提前感谢您的帮助。
答案 0 :(得分:2)
看起来你级联持续操作从员工到部门和转移。但他们也会将持久性操作级联到Employee Entity。
这意味着什么时候
$em->persist($an_employee);
你被困在一个持久循环中。
在我看来,级联持久化应该只有一种方式,即只在Employee实体上。
此外,如果您选择这样做,请将您的部门添加到员工,而不是相反:
$an_employee->addDepartment($a_department);
$an_employee->addTransfer($a_transfer);
这样,当你坚持一个员工时,它的部门和转移也应该坚持