这是我的prePersist on EventListener
public function prePersist(LifecycleEventArgs $args)
{
//the first entity will have the PMP, so we catch it and continue to skip this if after this
if ($this->pmp == null) {
$this->pmp = $args->getEntity()->getPmp();
}
$taxonomicClass = $args->getEntity();
if($taxonomicClass instanceof TaxonomicClass){
if(is_null($taxonomicClass->getId())){
//here it says that i have created a new entity, need to persist it via cascade={"persist"}
$taxonomicClass->setPmp($this->pmp);
}
}
}没关系,我在上面添加了注释:
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Pmp", cascade={"persist"})
* @ORM\JoinColumn(name="pmp_id", referencedColumnName="id", nullable=false)
**/
private $pmp;它保存了我的层次结构中的所有东西,甚至是一个新的PMP,一个已经存在于数据库中的对象!
我想要的是,从我的层次结构中保存的所有东西都需要与我通过的PMP相关,但是当我设置$taxonomicClass->setPmp($this->pmp);原则时,我认为我创建了PMP的一个新实例,因为im不是,我只是想让这个家伙与PMP有一个关联。
我试过将merge放在级联选项上,但它只适用于persist,如何使原则不创建新实例,而使用我传递的实例呢?
发布于 2015-11-03 11:49:17
注意到我的问题,我从记忆中分配了一个属性,我应该把他从数据库检索到理论理解。
public function prePersist(LifecycleEventArgs $args)
{
if ($this->pmp == null) {
$this->pmp = $args->getEntity()->getPmp();
}
$taxonomicClass = $args->getEntity();
if($taxonomicClass instanceof TaxonomicClass){
if(is_null($taxonomicClass->getId())){
//this solved the problem
$pmp = $args->getEntityManager()->getRepository("AppBundle:Pmp")->find($this->pmp->getId());
$taxonomicClass->setPmp($pmp);
}
}
}现在我要记住,当创建一个新实体时,但是它不需要保存,您必须从db检索它,cascade={"persist"}甚至不是必需的
https://stackoverflow.com/questions/33442982
复制相似问题