在 Zend Framework 2 项目中,我们有两个 Doctrine 2 实体,我们想从已经保存在数据库中的集合中删除一个元素......
所以我们有一个名为 FormGroupConstraint 的第一个实体:
/**
 * 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;
     }
}
以及名为 FormQuestionConstraint 的子实体:
/**
 * FormQuestionConstraint
 *
 * @ORM\Table(name="form_question_constraint")
 * @ORM\Entity(repositoryClass="Application\Dao\FormQuestionConstraintDao")
 */
class FormQuestionConstraint
{
    /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer", nullable=false)
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="IDENTITY")
    */
    protected $id;
    /**
    * @var \Application\Entity\FormGroupConstraint
    *
    * @ORM\ManyToOne(targetEntity="Application\Entity\FormGroupConstraint", cascade=   {"persist", "merge", "refresh", "remove"})
    * @ORM\JoinColumns({
    *   @ORM\JoinColumn(name="form_group_constraint_id", referencedColumnName="id")
    * })
    */
    protected $groupConstraint;
}
因此,如果我们尝试创建、持久化、刷新 FormGroupConstraint 实体,没问题,但是当我们想要删除 $constraints ArrayCollection 的元素时,什么也没有发生……
我们正在使用在 dev-master 中安装了 composer.phar 的 zend 2 的教义-orm-module ...
这是我们正在尝试做的一个例子:
$constraint = $this->findConstraintByGroup(1);
$formGroupConstraint = $this->_em->findOneById(1);
$formGroupConstraint->getConstraints()->removeElement($constraint);
$this->_em->persist($formGroupConstraint);
$this->_em->flush();
没有错误,但没有删除或删除......如果我们在persist()之前var_dump()getConstraints(),实际上元素仍在ArrayCollection中......
谁能解释我们如何做到这一点或为什么不删除该元素?