使用 Symfony 序列化程序组件和 AbstractNormalizer::OBJECT_TO_POPULATE 反序列化一个实体数组
Deserialize a entities array with Symfony Serializer Component and AbstractNormalizer::OBJECT_TO_POPULATE
我已经反序列化为一个对象数组:
$encoders = [new JsonEncoder()];
$normalizers = [new ObjectNormalizer(), new GetSetMethodNormalizer(), new ArrayDenormalizer()];
$serializer = new Serializer($normalizers, $encoders);
$clients = $serializer->deserialize($myJson, 'App\Entity\Client[]', 'json');
并且还将一个实体反序列化为现有实体(在数据库中不插入但更新):
$clientDb = $clientRepository->find(1);
$client = $serializer->deserialize($myJson, Client::class, 'json', [AbstractNormalizer::OBJECT_TO_POPULATE => $clientDb ]);
但是当我想两者都做时,学说只插入数据库:
$clients = $serializer->deserialize($myJson, 'App\Entity\Client[]', 'json', [AbstractNormalizer::OBJECT_TO_POPULATE => $clientRepository->findAll()]);
我是不是漏掉了什么?
-- 我找到的官方文档没有提到:https://symfony.com/doc/current/components/serializer.html#deserializing-in-an-existing-object --
编辑:我设法通过将 json 解码到数组中手动完成,然后在其上循环,重新编码为 json 循环中的每个项目,最后反序列化它们。
但是,如果有一种不用 decode/loop/encode 的方法,我更愿意使用它!
从您提到的 documentation 中检查以下信息:
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.
因此基于此,不支持一次填充对象数组。你必须遍历你的数组。
我已经反序列化为一个对象数组:
$encoders = [new JsonEncoder()];
$normalizers = [new ObjectNormalizer(), new GetSetMethodNormalizer(), new ArrayDenormalizer()];
$serializer = new Serializer($normalizers, $encoders);
$clients = $serializer->deserialize($myJson, 'App\Entity\Client[]', 'json');
并且还将一个实体反序列化为现有实体(在数据库中不插入但更新):
$clientDb = $clientRepository->find(1);
$client = $serializer->deserialize($myJson, Client::class, 'json', [AbstractNormalizer::OBJECT_TO_POPULATE => $clientDb ]);
但是当我想两者都做时,学说只插入数据库:
$clients = $serializer->deserialize($myJson, 'App\Entity\Client[]', 'json', [AbstractNormalizer::OBJECT_TO_POPULATE => $clientRepository->findAll()]);
我是不是漏掉了什么?
-- 我找到的官方文档没有提到:https://symfony.com/doc/current/components/serializer.html#deserializing-in-an-existing-object --
编辑:我设法通过将 json 解码到数组中手动完成,然后在其上循环,重新编码为 json 循环中的每个项目,最后反序列化它们。 但是,如果有一种不用 decode/loop/encode 的方法,我更愿意使用它!
从您提到的 documentation 中检查以下信息:
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.
因此基于此,不支持一次填充对象数组。你必须遍历你的数组。