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
Post a Comment