Symfony 不会从集合中删除实体
Symfony does not remove entity from collection
我知道通常有很多关于此主题的帖子。不幸的是,那些主要处理对数据库的实际持久操作。在我的例子中,我有一个问题发生在持久操作之前:
我有一个带有(原则)实体持久性集合的表单。您可以通过 javascript 从 DOM 中删除 "objects"。提交后,当在表单上调用 handleRequest 时,我的实体中的函数被调用,该函数从对象本身的集合中删除实体,并且它被调用,因为我可以在调试器中检查:
/**
* Remove prices
*
* @param \Whizzpm\Bundle\Entity\Supplier\SupplierPrice $prices
*/
public function removePrice(\Whizzpm\Bundle\Entity\Supplier\SupplierPrice $prices)
{
$this->prices->removeElement($prices);
}
这是 $prices 的定义:
/**
* @var
* @ORM\OneToMany(targetEntity="SupplierPrice", mappedBy="priceList", cascade={"all"})
*/
private $prices;
基本思想是将更新后的实体与其之前的状态进行比较,但在上述函数完成后,实体仍在集合中。
为了更精确:如果我在 "removeElement($prices)" 结束后立即检查 $this,它仍然包含应该删除的对象。
也许这很重要:
供应商(主要实体)
- 价目表(主要实体的属性 - 也是实体本身)
- 价格(价格表的属性,实体集合(价格项目)
prices 是应该删除元素(价格项)的集合。
对此有什么想法吗?我可以在这个问题上添加您需要的任何信息,我只是不知道,其中哪些是有意义的,因为有很多负载。
最后我在这个post中找到了解决方案:
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;
我知道通常有很多关于此主题的帖子。不幸的是,那些主要处理对数据库的实际持久操作。在我的例子中,我有一个问题发生在持久操作之前:
我有一个带有(原则)实体持久性集合的表单。您可以通过 javascript 从 DOM 中删除 "objects"。提交后,当在表单上调用 handleRequest 时,我的实体中的函数被调用,该函数从对象本身的集合中删除实体,并且它被调用,因为我可以在调试器中检查:
/**
* Remove prices
*
* @param \Whizzpm\Bundle\Entity\Supplier\SupplierPrice $prices
*/
public function removePrice(\Whizzpm\Bundle\Entity\Supplier\SupplierPrice $prices)
{
$this->prices->removeElement($prices);
}
这是 $prices 的定义:
/**
* @var
* @ORM\OneToMany(targetEntity="SupplierPrice", mappedBy="priceList", cascade={"all"})
*/
private $prices;
基本思想是将更新后的实体与其之前的状态进行比较,但在上述函数完成后,实体仍在集合中。
为了更精确:如果我在 "removeElement($prices)" 结束后立即检查 $this,它仍然包含应该删除的对象。
也许这很重要:
供应商(主要实体)
- 价目表(主要实体的属性 - 也是实体本身)
- 价格(价格表的属性,实体集合(价格项目)
prices 是应该删除元素(价格项)的集合。
对此有什么想法吗?我可以在这个问题上添加您需要的任何信息,我只是不知道,其中哪些是有意义的,因为有很多负载。
最后我在这个post中找到了解决方案:
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;