What is the most efficient way to convert my Symfony2 entity to an array ? Entity contains protected fields with setters/getters. Is it possible to do with JMSSerializer
?
相关问题
- Symfony2 Set Controller in the kernelControllerEve
- Webpack Encore: cannot import local dependencies
- Render custom attribute KNP Menu
- Problems with cap deploy a symfony2 project, can
- Allow CORS on symfony 4
相关文章
- Symfony : Doctrine data fixture : how to handle la
- Symfony is linked to the wrong PHP version
- Symfony2: check whether session exists or not
- Is there a way to modify the entity mapping config
- symfony2 form choice and mongodb
- Symfony 3.1 and OneUpUploaderBundle + Blueimp = Up
- Symfony does not remove entity from collection
- Symfony: Inject object (not service) to service co
Using this bundle is the most efficient way to convert Entities to serialized format. Moreover, it's recommended by Sensio Labs.
To serialize You need only to install, configure this bundle and then:
And deserialize:
Nothing more.
You can also use it to convert an object to an array:
Also, you can prevent infinite recursion using serialization groups:
Regards
If you have not installed Symfony Serializer Component.
install it
composer require symfony/serializer
then just convert any entity to array as follows.
and the
$data = array:[ "success" => true "errorMessage" => null "result" => "1" "type" => "url" ]
You can also just create a public routine in the entity itself which is similar to what serializer is doing.
Using JMSSerializer for such a simple task seems like an overkill to me. I would use Symfony Serializer Component. The demo page shows how to serialize an entity to JSON.
If you just want to put it to array, you don't need serialization at all, you could just instantiate
GetSetMethodNormalizer
and use it since component uses arrays as normalized format.