12

在 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中......

谁能解释我们如何做到这一点或为什么不删除该元素?

4

3 回答 3

32

也许有点晚了,但尝试添加orphanRemoval=true到关系的反面(OneToMany)

@ORM\OneToMany(
    targetEntity="Application\Entity\FormQuestionConstraint",
    mappedBy="groupConstraint",
    cascade={"persist", "remove"},
    orphanRemoval=true
)
于 2013-12-21T17:20:00.967 回答
5

您可以在关系orphanRemoval=trueOneToMany一侧添加,例如:

@ORM\OneToMany(
    targetEntity="Application\Entity\FormQuestionConstraint",
    mappedBy="groupConstraint",
    cascade={"all"},
    orphanRemoval=true
)

出现这种行为的原因是实体从 ArrayCollection 中移除,但集合的内容仅由指向父实体的子实体的外键决定。

当学说下一次查找父实体的子实体时,它仍然存在并且将再次显示在下一个请求中,当再次从数据库中获取 ArrayCollection 时。

如果orphanRemoval设置为true,则此类子实体将被删除persist()

于 2015-05-29T07:18:10.123 回答
3

因为引用被保留,FormQuestionConstraint你需要做:

$this->_em->remove($constraint);
$this->_em->flush();
于 2013-04-19T17:18:18.920 回答