V
V
Valery2014-03-04 09:41:04
symfony
Valery, 2014-03-04 09:41:04

How to not create already existing entities when deserializing a Doctrine2 object?

Good afternoon,
Symfony2.3
ORM - Doctrine2
Serialization - JMSSerializeBundle
I deserialize an object like this:

<entry>
    <title><![CDATA[Тестовый объект]]></title>
    <address_district>
      <id>1</id>
      <title><![CDATA[Западный Округ]]></title>
    </address_district>
</entry>

address_district - A field with a ManyToMany relationship to the District entity.
District with ID=1 is already in the database, but on persist() the doctrine re-creates this nested object and assigns a new ID. How can this be avoided in such a way that an already existing object is used?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valery, 2014-03-04
@Akuma

Found the solution myself :)
Instead of persist() on a deserialized entity, merge() should be used .
Just keep in mind that events called by persist() will not work in this case.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question