0

I already deserialize into a array of object with :

$encoders = [new JsonEncoder()];
$normalizers = [new ObjectNormalizer(), new GetSetMethodNormalizer(), new ArrayDenormalizer()];
$serializer = new Serializer($normalizers, $encoders);

$clients = $serializer->deserialize($myJson, 'App\Entity\Client[]', 'json');

And also deserialize one entity into existing one (to no INSERT but UPDATE in db) :

$clientDb = $clientRepository->find(1);
$client = $serializer->deserialize($myJson, Client::class, 'json', [AbstractNormalizer::OBJECT_TO_POPULATE => $clientDb ]);

But when I want to do both, doctrine only insert in DB:

$clients = $serializer->deserialize($myJson, 'App\Entity\Client[]', 'json', [AbstractNormalizer::OBJECT_TO_POPULATE => $clientRepository->findAll()]);

Did I miss something ?

-- The official doc I found did not mention it: https://symfony.com/doc/current/components/serializer.html#deserializing-in-an-existing-object --

EDIT: I manage to do it manually with decode json into array, then loop on it, re-encode into json each item in loop and finally deserialize them. But if there is a way to do it without decode/loop/encode I prefer to use it !

1 Answer 1

1

Check the following information from the documentation you mentioned yourself:

The AbstractNormalizer::OBJECT_TO_POPULATE is only used for the top level object. If that object is the root of a tree structure, all child elements that exist in the normalized data will be re-created with new instances.

When the AbstractObjectNormalizer::DEEP_OBJECT_TO_POPULATE option is set to true, existing children of the root OBJECT_TO_POPULATE are updated from the normalized data, instead of the denormalizer re-creating them. Note that DEEP_OBJECT_TO_POPULATE only works for single child objects, but not for arrays of objects. Those will still be replaced when present in the normalized data.

So based on that it is not supported to populate an array of objects at once. You'll have to go through your array.

Sign up to request clarification or add additional context in comments.

6 Comments

I read this part but what I understood is "OBJECT_TO_POPULATE is only used for the top level object" = it only work for primary entity and not for relations, am I wrong ? (because they do not talk about array ?) And for the part "DEEP_OBJECT_TO_POPULATE only works for single child objects, but not for arrays of objects" = Same thing you said, not working with array IF you want to populate all child element (child element = relations ?).
You're not talking about relations at all, are you?
In my case no, I understood that it's impossible to deserialize relations so I even not trying ^^ What I understood for OBJECT_TO_POPULATE is that I can use it for a array but not for relations. And it's the contrary for DEEP_OBJECT_TO_POPULATE
Sorry, I don't get your question then.
Thanks for your help and time in any case ! Maybe I will wait for someone who use this two options in real case.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.