pele_mele:stack_exchange:stack_overflow:35478874
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédente | |||
| pele_mele:stack_exchange:stack_overflow:35478874 [2024/11/23 04:02] – supprimée - modification externe (Date inconnue) 127.0.0.1 | pele_mele:stack_exchange:stack_overflow:35478874 [2024/11/23 04:02] (Version actuelle) – ↷ Nom de la page changé de pele_mele:stack_exchange:stack_overflow:stackoverflow-35478874 à pele_mele:stack_exchange:stack_overflow:35478874 alexis | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | ====== How to choose the driver to serialize/ | ||
| + | I have an object which I serialize using annotations. It works fine. | ||
| + | |||
| + | If I am using yaml configuration, | ||
| + | |||
| + | My problem is I want to use both in different context. Lets say that in controller one, I want to use the annotation configuration and in controller two, I want to use the yaml configuration. I want to do that because I need to have different field names in those outputs. | ||
| + | |||
| + | * I tried to override the serializer with a new instance using only the annotation. | ||
| + | * I change jmsserializer service configuration to not use specific drivers. It worked but I cannot choose which one to activate dynamically. | ||
| + | * I tried to select the driver in the container but I couldn' | ||
| + | |||
| + | Is this possible? Am I missing something? | ||
| + | |||
| + | <WRAP help> | ||
| + | I don't see how you can achieve this. | ||
| + | |||
| + | But if you to want expose a property differently, | ||
| + | |||
| + | Example : | ||
| + | |||
| + | <code php> | ||
| + | /** | ||
| + | * @JMS\ExclusionPolicy(" | ||
| + | * @ORM\Entity | ||
| + | */ | ||
| + | class FooBar | ||
| + | { | ||
| + | /** | ||
| + | * @ORM\Column(type=" | ||
| + | * @JMS\Groups({" | ||
| + | */ | ||
| + | protected $name; // output ' | ||
| + | |||
| + | /** | ||
| + | * @ORM\Column(type=" | ||
| + | * @JMS\SerializedName(" | ||
| + | * @JMS\Accessor(getter=" | ||
| + | * @JMS\Groups({" | ||
| + | */ | ||
| + | protected $fooName; // output ' | ||
| + | |||
| + | // ... | ||
| + | |||
| + | public function setName($name) | ||
| + | { | ||
| + | $this-> | ||
| + | |||
| + | return $this; | ||
| + | } | ||
| + | |||
| + | public function getName() | ||
| + | { | ||
| + | return $this-> | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Like this, the property can be serialised in two different names : | ||
| + | |||
| + | <code php> | ||
| + | use JMS\Serializer\SerializationContext; | ||
| + | |||
| + | $serializer-> | ||
| + | |||
| + | //will output $name as ' | ||
| + | |||
| + | $serializer-> | ||
| + | |||
| + | //will output $fooName as ' | ||
| + | </ | ||
| + | |||
| + | Note the '' | ||
| + | |||
| + | See more at the [[http:// | ||
| + | |||
| + | Hope this could be an alternative for you. | ||
| + | </ | ||
| + | <WRAP info> | ||
| + | [[https:// | ||
| + | </ | ||
