Symfony不会从集合中删除实体

时间:2015-01-05 00:28:42

标签: php symfony doctrine-orm

我知道这个主题上有很多帖子。不幸的是,那些主要处理对数据库的实际持久操作。在我的情况下,我遇到了在持续操作之前发生的问题:

我有一个带有(Doctrine)persistenceCollection实体的表单。你可以删除"对象"从DOM通过JavaScript。在提交之后,当在表单上调用handleRequest时,调用我的实体中的函数,该函数从对象本身的集合中删除实体,然后调用它,因为我可以在调试器中检查:

/**
 * Remove prices
 *
 * @param \Whizzpm\Bundle\Entity\Supplier\SupplierPrice $prices
 */
public function removePrice(\Whizzpm\Bundle\Entity\Supplier\SupplierPrice $prices)
{
    $this->prices->removeElement($prices);
}

这是$ price的定义:

 /**
 * @var
 * @ORM\OneToMany(targetEntity="SupplierPrice", mappedBy="priceList", cascade={"all"})
 */
private $prices;

基本思想是将更新后的实体与之前的状态进行比较,但在上述功能完成后,权利仍在集合中。

为了更加精确:如果我在" removeElement($ prices)"之后检查$ this。通过,它仍然包含应该被删除的对象。

也许这很重要:

供应商(主要实体)

  • 价格表(主要实体的财产 - 也是实体本身)
    • 价格(价目表的财产,实体的收集(价格项目)

价格是应该删除元素(价格项)的集合。

对此有何想法?我可以在这个问题上添加你需要的任何信息,我只是不知道,哪些是有意义的,因为有负载。

1 个答案:

答案 0 :(得分:20)

最后我在这篇文章中找到了一个解决方案:

removeElement() and clear() doesn't work in doctrine 2 with array collection property

我必须在拥有实体中取消设置相应的值:

public function removePrice(\Whizzpm\Bundle\Entity\Supplier\SupplierPrice $prices)
{
    $this->prices->removeElement($prices);
    $prices->setPriceList(null);
}

并将orphanRemoval = true添加到实体集合

/**
 * @var
 * @ORM\OneToMany(targetEntity="SupplierPrice", mappedBy="priceList", cascade={"all"}, orphanRemoval=true)
 */
private $prices;