Symfony不会从集合中删除实体

Fuz*_*zel 10 php symfony doctrine-orm

我知道这个话题上有很多帖子.不幸的是,那些主要处理对数据库的实际持久操作.在我的情况下,我有一个问题发生在persist-operation之前:

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

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

这是$ price的定义:

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

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

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

也许这很重要:

供应商(主要实体)

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

price是应该删除元素(price item)的集合.

有什么想法吗?我可以在这个我不知道的问题上添加你需要的任何信息,哪个有意义,因为有负载.

Fuz*_*zel 21

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

removeElement()和clear()在带有数组集合属性的doctrine 2中不起作用

我也必须取消拥有实体中的相应值:

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

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

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

  • 有趣的是,我以前从未听说过"orphanRemoval".我猜它隐藏在Symfony文档中的某个地方.搜索它只会给我其他人遇到问题的结果.我有同样的问题(现在和早期的项目),在我的情况下,我唯一要做的就是添加`orphanRemoval = true`; 无需设置对"null"的其他引用.tyvm (3认同)