4

I am wondering if there is a way to change (or define inside) annotations in a child class inheriting a MappedSuperClass, for example, let say we have a class BaseUser (mappedsuperclass), a child class User :

<?php
...
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
...


/**
* @ORM\MappedSuperclass 
*/
class BaseUser
{
    /**
     * @ORM\Column(name="firstname", type="string", length=100)
     * @Serializer\SerializedName("First_Name")
     * @Serializer\Expose
     * @Serializer\Type("string")
     * @Serializer\Groups({"Basic"})
     */
    protected $firstName;
}

/**
* @ORM\Entity
*/
class User extends BaseUser
{
    /**
     * @ORM\Column(name="sign", type="string", length=50)
     */
    private $sign;
}

What I would like to do is either defining the "Serializer" annotations from User class directly (but let the property firstName to be defined in the BaseUser class), OR, overriding the definition of Serialize from the User class.

I did not found anything about this topic, has someone already figured it out? Thanks

Bil5
  • 502
  • 1
  • 5
  • 15

1 Answers1

0

You can tell the JMS Serializer what to expose or not in your config.

app/config/config.yml:

jms_serializer:
metadata:
    directories:
        - { path: %kernel.root_dir%/Resources/FOSUserBundle/serializer, namespace_prefix: 'FOS\UserBundle' }

app/Resources/FOSUserBundle/serializer/Model.User.yml:

FOS\UserBundle\Model\User:
exclusion_policy: ALL
properties:
    id:
        expose: true
    email:
        expose: true
    username:
        expose: true
    enabled:
        expose: true
    locked:
        expose: true

Source: https://github.com/schmittjoh/JMSSerializerBundle/issues/78#issuecomment-31831236

Max Dum
  • 181
  • 1
  • 1
  • 8