symfony - Include a method when object is serialized in JMS -


i have method returns value:

/** * @orm\table() * @orm\entity(repositoryclass="personrepository") */ class person {     /**      * @var integer      *      * @orm\column(name="id", type="integer")      * @orm\id      * @orm\generatedvalue(strategy="auto")      */    private $id;     public function getfoo(){       return $this->id + 1;    }   //setters & getters  } 

i include value getfoo() returns when serialize person object this:

{    'id' : 25    'foo' : 26 } 

you need set @virtualproperty , @serializedname.

use jms\serializer\annotation\virtualproperty; use jms\serializer\annotation\serializedname;  class person {    ....    ....    ....     /**     * @virtualproperty     * @serializedname("foo")     */    public function getfoo(){       return $this->id + 1;    }      ....     ....     .... } 

you can read more here: http://jmsyst.com/libs/serializer/master/reference/annotations

pay attention works serialization , not deserialization.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -