Skip to content
Advertisement

Symfony – creating new entity instead of updating existing

I have the following structure:

Category property that contains link to property and its value:

JavaScript

Abstract property value type with a discriminator map:

JavaScript

And a sample concrete value type:

JavaScript

For some reason, when form is submitted, instead of updating a value for IntegerValue, a new entity gets created, and new row in properties_values__value_entry / properties_values__integer_value. I tried tracking through the $this->em->persist($entity), where $entity is CategoryProperty, and it seems that IntegerValue gets marked as dirty and created anew. How can I track the cause of this happening? My form processing is pretty standard:

JavaScript

UPDATE #1 What I already tried: Retrieve category property by ID from entity manager through

JavaScript

Altogether it seems this may be related to the fact that I have a dynamic form being created based on the selection. When I add a category property, I display a dropdown with a list of property types (integer, string, area, volume etc), and after selection a new form for that property is displayed. Though this works fine and adds new property without a problem, it seems that the code for EDITING same property is missing something, and instead of update it creates it anew.

Advertisement

Answer

Thanks to everyone participating, I have been reading through Symfony documentation and came across the ‘by_reference’ form attribute. Having considered that my form structure overall looks like this:

JavaScript

for the form, I decided to set it to true in PropertyValueType configureOptions method. As it is explained in the documentation, with it being set to false, the entity is getting cloned (which in my case is true), thus creating a new object at the end.

Note that I’m still learning Symfony and will be refining the answer when I get a better understanding of what’s going on behind the scenes.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement