Search code examples
phpsymfonyjmsserializerbundlejms-serializer

JMSSerializer and inheritance Class


I have got a problem with JMS Serializer and inheritance class. When I serialize my entities it doesn't take care of JMSSerializer Annotation because of inheritance class... So How could I set JMSSerializer Annocation with class inheritance ?

Example :

/**
 * Class Category
 *
 * @Serializer\ExclusionPolicy("all")
 */
class Category extends BaseCategory
{
    /**
     * @var integer $id
     *
     * @Serializer\Expose
     */
    protected $id;
}


/**
 * Class BaseCategory
 */
class BaseCategory
{
    /**
     * @var Dish
     */
    protected $dishs;

    /**
     * @var string
     */
    protected $name;
}

When I serialize Category, the json returned looks like :[{"dishs":[{"name":"Salade","id":5}],"name":"...","id":1}]. The ExclusionPolicy annotation is not applied.

Do you have any ideas why ? If I set all fields in the same entity, it works good, but it breaks all my template ...

Thanks


Solution

  • You inherit from BaseCategory - you cannot expect this to work. Set your ExclusionPolicy in your base class - in this case your extended class will inherit from BaseCategory.