Serializing Entity Relation only to Id with JMS Serializer

后端 未结 6 543
温柔的废话
温柔的废话 2021-02-02 09:54

I\'m trying to serialize a entity relation with JMS Serializer.

Here is the Entity:

class Ad
{ 

    /**
     * @Type(\"string\")
     * @Groups({\"manag         


        
6条回答
  •  生来不讨喜
    2021-02-02 10:20

    Yes, you could use @VirtualProperty annotation:

    /**
     * @VirtualProperty
     * @SerializedName("foo")
     */
    public function bar()
    {
        return $this->country->getCode();
    }
    

    But be aware when it comes to deserialization:

    @VirtualProperty This annotation can be defined on a method to indicate that the data returned by the method should appear like a property of the object.

    > Note: This only works for serialization and is completely ignored during deserialization.

    Hope this helps...

提交回复
热议问题