That's what serialization groups are meant for. They're similar to validation groups in the validator component.
JMSSerializerBundle, how to choose alternative config file
-
29-06-2022 - |
Вопрос
I'm using JMS serializer as the JMSSerializerBundle for Symfony2.
I serialize my entities this way :
$this->serializer = $this->container->get('jms_serializer');
$json = $this->serializer->serialize($values, 'json');
The bundle use automatically the ref config that match the bundle/entity name :
MyBundlePath/Resources/config/serializer/Entity.Name.yml
In my case, i need to provide two different configuration for the same entity class, is there any way to specify which yml/xml file to use to the serializer ?
Edit: nifr provided the solution, the group features
In my YAML conf case, you just need to add the groups param
Entity.ClassName:
...
properties:
some-property:
...
groups: [first, second]
Then specify it in the serialize call :
$serializer->serialize($values, 'json', SerializationContext::create()->setGroups(array('my-group-name')));
Решение
Не связан с StackOverflow