Doctrine orm 从一个上的ArrayCollection中删除元素在persist()上许多人不会删除它吗?
在Zend Framework 2项目中,我们得到了两个Doctrine 2实体,我们希望从保存在数据库中的集合allready中删除一个元素 因此我们有了第一个名为FormGroupConstraint的实体:Doctrine orm 从一个上的ArrayCollection中删除元素在persist()上许多人不会删除它吗?,doctrine-orm,Doctrine Orm,在Zend Framework 2项目中,我们得到了两个Doctrine 2实体,我们希望从保存在数据库中的集合allready中删除一个元素 因此我们有了第一个名为FormGroupConstraint的实体: /** * FormGroupConstraint * * @ORM\Table(name="form_group_constraint") * @ORM\Entity(repositoryClass="Application\Dao\FormGroupConstraintDa
/**
* FormGroupConstraint
*
* @ORM\Table(name="form_group_constraint")
* @ORM\Entity(repositoryClass="Application\Dao\FormGroupConstraintDao")
*/
class FormGroupConstraint {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @param \Doctrine\Common\Collections\ArrayCollection
* @ORM\OneToMany(targetEntity="Application\Entity\FormQuestionConstraint", mappedBy="groupConstraint", fetch="EAGER", cascade={"persist", "merge", "refresh", "remove"})
*/
protected $constraints;
public function __construct()
$this->constraints = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @param \Doctrine\Common\Collections\ArrayCollection $constraints
*/
public function addConstraints($constraints) {
foreach ($constraints as $constraint) {
$this->constraints->add($constraint);
}
return $this->constraints;
}
/**
* @param \Doctrine\Common\Collections\ArrayCollection $constraints
*/
public function removeConstraints($constraintsToRemove) {
foreach ($constraintsToRemove as $key => $constraintToRemove) {
$this->constraints->removeElement($constraintToRemove);
}
return $this->constraints;
}
}
$constraint = $this->findConstraintByGroup(1);
$formGroupConstraint = $this->_em->findOneById(1);
$formGroupConstraint->getConstraints()->removeElement($constraint);
$this->_em->persist($formGroupConstraint);
$this->_em->flush();
和名为FormQuestionConstraint的子实体:
因此,如果我们尝试创建、持久化、刷新FormGroupConstraint实体,没有问题,
但当我们想要删除$constraints ArrayCollection的元素时,什么都不会发生
我们正在使用zend 2的doctrine orm模块,该模块安装在dev master中的composer.phar中
下面是我们正在尝试做的一个例子:
/**
* FormGroupConstraint
*
* @ORM\Table(name="form_group_constraint")
* @ORM\Entity(repositoryClass="Application\Dao\FormGroupConstraintDao")
*/
class FormGroupConstraint {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* @param \Doctrine\Common\Collections\ArrayCollection
* @ORM\OneToMany(targetEntity="Application\Entity\FormQuestionConstraint", mappedBy="groupConstraint", fetch="EAGER", cascade={"persist", "merge", "refresh", "remove"})
*/
protected $constraints;
public function __construct()
$this->constraints = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @param \Doctrine\Common\Collections\ArrayCollection $constraints
*/
public function addConstraints($constraints) {
foreach ($constraints as $constraint) {
$this->constraints->add($constraint);
}
return $this->constraints;
}
/**
* @param \Doctrine\Common\Collections\ArrayCollection $constraints
*/
public function removeConstraints($constraintsToRemove) {
foreach ($constraintsToRemove as $key => $constraintToRemove) {
$this->constraints->removeElement($constraintToRemove);
}
return $this->constraints;
}
}
$constraint = $this->findConstraintByGroup(1);
$formGroupConstraint = $this->_em->findOneById(1);
$formGroupConstraint->getConstraints()->removeElement($constraint);
$this->_em->persist($formGroupConstraint);
$this->_em->flush();
没有错误,但没有删除或删除。。。
如果我们在persist()之前对getConstraints()进行var_dump(),实际上元素仍在ArrayCollection中
有人能解释一下我们如何做到这一点,或者为什么不删除元素吗?因为引用保存在
FormQuestionConstraint
上,您需要执行以下操作:
$this->_em->remove($constraint);
$this->_em->flush();
可能有点晚了,但尝试向关系的反面(一方)添加
orphaneremovation=true
@ORM\OneToMany(
targetEntity="Application\Entity\FormQuestionConstraint",
mappedBy="groupConstraint",
cascade={"persist", "remove"},
orphanRemoval=true
)
您可以在关系的
OneToMany
一侧添加orphaneremovation=true
,例如:
@ORM\OneToMany(
targetEntity="Application\Entity\FormQuestionConstraint",
mappedBy="groupConstraint",
cascade={"all"},
orphanRemoval=true
)
此行为的原因是该实体已从ArrayCollection中删除,但集合的内容仅由子实体指向父实体的外键确定
当下次doctrine查找父实体的子实体时,它仍然在那里,并且将在再次从数据库获取ArrayCollection时再次显示在下一个请求中
如果
orphanRemoving
设置为true
,这些子实体将在persist()
时被删除,感谢您的回复,但问题是,当我们尝试循环ArrayCollection以查看哪些实体被删除时,数据库和当前ArrayCollection之间没有任何更改。。。任何removeElement($constraint)都是无用的???为什么此方法不在ArrayCollection上执行任何操作?(Hibernate列表或其他ORM集合容器可以这样工作…)即使调用flush()
?,对ArrayCollection
也没有任何更改??如果没有,请尝试调用$this->\u em->flush()
。我们要做的是:1:加载$formGroupConstraint 2:通过调用$formGroupConstraint->getConstraints()->removeElement($constraint)删除元素;3:最后,执行$this->\u em->persist($formGroupConstraint)$此->\u em->flush();。但不会从集合或数据库中删除任何内容。这个过程可行吗?来自:“条令将只检查关联的拥有方是否有更改。”因为引用保存在FormQuestionConstraint
上,所以您需要按照我的回答删除它。这是一个比第一个更好的解决方案,不需要额外的代码。对于其他用户(如我)的YAML来说:孤儿移除:在“左”字段中的“一夫一妻”关系是正确的。为什么很难为symfony或doctrine找到任何东西?嘿,谢谢,这是一个很好的答案!小心,很多人都没有删除属性,这必须在一方