Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMS Serialize @VirtualProperty doesn't work

I'm trying to add a method to serialize, but JMS Serialize does not show the field.

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

class Ads
{   
    /**
     * @VirtualProperty
     * @Type("string")
     * @SerializedName("Foo")
     * @Groups({"manage"})
     */
    public function foo(){
        $foo = 'foo';
        return $foo;
    }
    ...
}

And then:

use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerBuilder;

...

$context = new SerializationContext();
$context->setGroups(array('manage'));

$serializer = JMS\Serializer\SerializerBuilder::create()->build();
$jsonContent = $serializer->serialize($ad, 'json', $context);

I have not seen any examples of how to use VirtualProperty.

Is the syntax correct? What is wrong?

Thank you.

like image 748
escrichov Avatar asked Apr 15 '13 15:04

escrichov


1 Answers

I've noticed the problem. There was created before the object "$ ad". My fault. Virtual property functioning properly.

like image 178
escrichov Avatar answered Oct 18 '22 23:10

escrichov