Deserialize an entity with a relationship with Symfony Serializer Component

前端 未结 5 1783
挽巷
挽巷 2020-12-16 01:27

I\'m trying to deserialize an entity with a relationship using the symfony serializer component. This is my entity:

namespace AppBundle\\Entity;

use Doctrin         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 01:42

    This is what the Symfony documentation calls "Recursive Denormalization", starting from version 3.3 up to the actual master, 4.0.

    In order for Symfony to find the property types of the serialized objects, it needs to use the PropertyInfo component, which, as @slk500 stated in his answer, has to be activated in the framework configuration.

    So, if you are using the full framework, all you need to do in order to deserialize nested json objects is this:

    1.Enable the serializer and the property info components in config.yml:

    framework:
        #...
        serializer: { enabled: true }
        property_info: { enabled: true }
    
    1. Then inject the serializer wherever you need it:
    deserialize($request->getContent(), 'AppBundle\Entity\Document', 'json');
            // ...
        }
    }
    

    The default features of these components were enough for my needs.
    Autowiring takes care of the basic service declaration, so unless you need specific normalizers, you don't even have to edit the services.yml configuration file. Depending on your use cases, you may have to enable specific features. Check the Serializer and PropertyInfo documentation for (hopefully) more specific use cases.

提交回复
热议问题