Symfony不会从实体中移除实体 [英] Symfony does not remove entity from collection

查看:132
本文介绍了Symfony不会从实体中移除实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有关这个话题的帖子一般很多。不幸的是,这些主要处理数据库的实际持续操作。在我的情况下,我有一个在持久化操作之前发生的问题:



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

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

这是$ price的定义:

  / ** 
* @var
* @ ORM\OneToMany(targetEntity =SupplierPrice,mappedBy =priceList cascade = {all})
* /
私人$价格;

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



为了使这一点更精确:如果在removeElement($ price)通过后检查$这个权限,它仍然包含刚刚被删除的对象



也许这很重要:



供应商(主要实体)




  • 价目表(主要实体的财产 - 也是实体本身)

    • 价格(价目表的财产,实体的收集(价格项目)




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



有什么想法吗?我可以添加任何您需要的信息,我不知道这是什么,哪些是有道理的,那里有负担。

解决方案

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



一个href =https://stackoverflow.com/questions/6462024/removeelement-and-clear-doesnt-work-in-doctrine-2-with-array-collection-pro> removeElement()和clear()doesn' Ť在doctrine 2中与数组收集属性一起工作



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

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

,并添加orphanRemoval = true为实体集合

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


I know there are loads of posts on this topic in general. Unfortunately those mostly deal with the actual persist-operation to the database. In my case I have a problem that happens before the persist-operation:

I have a form with a (Doctrine) persistenceCollection of entities. You can remove "objects" from the DOM via javascript. After submit, when handleRequest is called on the form, the function in my entity is called which removes the entity from the collection in the object itself, and it is called as I can check in the debugger:

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

And this is the definition of $prices:

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

The basic idea is to compare the updated entity with it's previous state but after the function above has finished the entitiy is still in the collection.

To make this more precise: If I check $this right after the "removeElement($prices)" is through, it still contains the object that just should have been removed.

Maybe this is important:

supplier (main entity)

  • pricelist (property of main entity - also entity itself)
    • prices (property of pricelist, collection of entities (price items)

prices is the collection of which the element (price item) should be removed.

Any thoughts on this? I can add any information you need on this question I just don't know, which of it makes sense, sincer there are loads.

解决方案

Finally I found a solution in this post:

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

I have to unset the corresponding value in the owning entity too:

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

and add orphanRemoval=true to the entity collection

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

这篇关于Symfony不会从实体中移除实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆