Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony does not remove entity from collection

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.

like image 266
Fuzzzzel Avatar asked Jan 05 '15 00:01

Fuzzzzel


1 Answers

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;
like image 173
Fuzzzzel Avatar answered Sep 27 '22 18:09

Fuzzzzel