I
I
Ivashka2015-07-29 10:11:19
symfony
Ivashka, 2015-07-29 10:11:19

How to merge two entities into one?

There are two entities, one in the database, the other comes from the client. How to combine these two entities into one so that the entity that came from the client updates the entity received from the database, but only with those properties that are not NULL for the client (coming) entity?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Ivashka, 2015-07-29
Kolbaskin @gee_danko

Found a solution through the Serializer component. It was updated in 2.7, where they added the option that is needed. Thanks to all!

A
Alexey Tsarapkin, 2015-07-29
@Dreamka

Use DI

S
Sergey, 2015-07-29
Protko @Fesor

the other comes from the client

umm.... with an external API? Or from the client through Request? if the latter, then it is not an entity, it is just some data.
In general, something like this:
1) we take the data from the request and make it a DTO (just some kind of object with fields, you can use public ones to make it easier, you can generally just use arrays or stdClass).
2) validate this DTO
3) if everything is fine, either we have a service that can update the entity through setters, or the entity has a method updateFromDTO($dto);
something like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question