Search code examples
phpsymfonytwigfosuserbundlefosrestbundle

Duplicate definition of column 'username' on entity FOSUserBundle FOSRestBundle JMSSerializerBundle


I am trying to use FOSUserBundle with FOSRestBundle and JMSSerializer.

I have successfully got the RESTFul modules working so i am able to access /api/v1/users/ however it returns all DB fields from FOSUserBundle and i want to be able to specify which ones to provide.

I have got this working on my other modules by adding @Expose to my annotation in the respective modules Entity. However there is no entity for the FOSUserBundle that i can see and when trying to create my own (provided below) it throws up this exception:

MappingException: Duplicate definition of column 'username' on entity 'CYLO\UserBundle\Entity\User' in a field or discriminator column mapping.

Any help appreciated.


Solution

  • You need to provide serialization definition for the class FOS\UserBundle\Model\User. For example, using YAML file named Model.User.yml:

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

    Then configure jms serializer like this:

    jms_serializer:
        metadata:
            auto_detection: false
            directories:
                FOSUserBundle:
                    namespace_prefix: FOS\UserBundle
                    path: "@MyOwnBundle/Resources/config/serializer"
                    ...