简体   繁体   中英

Symfony Serializer : select group and add extra attributes

With Symfony Serializer , I can select group with:

[
    'groups' => 'api_index',
]

I can select attributes with:

[
    'attributes' => [...],
]

I can ignore attributes with:

[
    'ignored_attributes' => [...],
]

But I want to select a group and add extra attributes .

Can you help me?

To add extra Attributes, you need to to use the @VirtualProperty .

use JMS\Serializer\Annotation\VirtualProperty;
use JMS\Serializer\Annotation\SerializedName;

class Entity
{
     private $field;

     /**
      * @VirtualProperty
      * @SerializedName("extra_field")
      */
     public function getextraField()
     {
         // do some logic
         // return "foo";

     }
}

your JSON data seems to be something like:

{
 ...,
"extra_field":"foo"
}

About @VirtualProperty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM