How Symfony works to remove an collection items

496 Views Asked by At

When I submit a collection with removed elements, my elements are correctly removed but, I don't understand how Symfony remove elements.

When I die(); the script doesn't stop into my remover method, so I think Symfony doesn't pass into my method. So, how Symfony can remove my elements without call my remover method ? How it's work ?

Application context :
I got some data from an API and I just want to add or remove data from my web interface. So, after I've add/remove data, I just want to post the new object on the API and remove the rows concerned. I also can compare the original object (get from the API) on pre submit and the new object on post submit to see the differences between both. But, if a method exist to call the remover of my object, I don't have to use this second solution and I think the first solution with a personnal remover is better.

Example of my remover :

public function removeMyElement($myElement)
{
    personnalMethodTreatment($myElement);
    if (!$this->myElement->contains($myElement)) {
        return;
    }
    $this->myElement->removeElement($myElement);
}

Thank's for your help !

SOLUTION

So, thank to all for the help. I write this lines to help other people who would have the same problem.

By default, Symfony call this own remover. If you want to call a custom remover, add the by_reference option at false to your collection form type.
by_reference: false
More information about by_reference https://symfony.com/doc/current/reference/forms/types/collection.html#by-reference

To conclude, by default Symfony call your custom method in this order :
Setter > Remover

So, if you have a setter and a remover, Symfony will prefer the former.

To solve my issu, I've followed the precedent instructions and I maked my specific treatment into my setter.

Hoping to have helped people.

0

There are 0 best solutions below