Add extra fields using JMS Serializer bundle

后端 未结 6 1405
说谎
说谎 2020-12-13 04:03

I\'ve an entity I usually serialize using the JMS Serializer bundle. I have to add to the serialization some fields that doesn\'t reside in the entity itself but are gathere

6条回答
  •  星月不相逢
    2020-12-13 04:45

    To further answer the original question. Here is how you limit added data for some serialized groups (in this example some_data is only added when we are not using the list group:

    public function onPostSerializeSomeEntityJson(ObjectEvent $event) {
    
        $entity = $event->getObject();
    
        if (!in_array('list', (array)$event->getContext()->attributes->get('groups'))) {
    
            $event->getVisitor()->addData('user_access', array(
                'some_data' => 'some_value'
            ));
        }
    }
    

    (array)$event->getContext()->attributes->get('groups') contains an array of the used serialized groups.

提交回复
热议问题