Search code examples
symfonyfosrestbundle

How can I use a DataTransformer transform with the FosRestBundle view layer


I've using a DataTransformer on my form to reverseTransform a decimal value send by the client into a Money/Money object.

This works well as expected, however when I return the data to the client using the FosRestBundle view layer, I'm not sure how I can use that transformer to transform it back into a decimal value for the client?


Solution

  • If you are using JMSSerializer with FOSRestBundle the serializer usually accesses the values of your object using reflection so the getters aren't touched.

    You can however set the access type for the property or all of the properties in the object to the public method (getters/setters) using the access_type setting.

    For Annotation you would use..

    use JMS\Serializer\Annotation\AccessType;
    
    class YourClass
    {
        /**
         * @AccessType("public_method")
         */
        private $money;
    
        ....
    }
    

    For more info see:
    Annotations: http://jmsyst.com/libs/serializer/master/reference/annotations#accessor
    XML: http://jmsyst.com/libs/serializer/master/reference/xml_reference
    YAML: http://jmsyst.com/libs/serializer/master/reference/yml_reference