By default Symfony 2.5 uses Doctrine and for each table it has a class with private properties that connected with table fields and get and set methods for each field. But I want to get all fields from one record without using get method for each field in loop. Because after that I have to send a JSON-object to front-end part. How can I do it? Because I don't want use get for each field, to get it value, I need values of all fields.
Asked
Active
Viewed 1,583 times
0
-
What you want is serializing your `Entity` into JSON. Take a look at this: http://stackoverflow.com/questions/6706485/how-to-encode-doctrine-entities-to-json-in-symfony-2-0-ajax-application. – FyodorX Sep 08 '14 at 09:02
-
I think this library doesn't support. – madberdin Sep 08 '14 at 09:16
-
Are there any other ideas? – madberdin Sep 08 '14 at 09:34
1 Answers
1
You can serialize Doctrine entities with the JMSSerializerBundle. It's exactly designed for this type of problem. It can serialize your entity based on the getters it finds in the entity class.

lxg
- 12,375
- 12
- 51
- 73
-
I made these steps composer require jms/serializer 0.13 $serializer = JMS\Serializer\SerializerBuilder::create()->build(); $serializer->serialize($object, 'json'); But Symfony returns me an error. – madberdin Sep 08 '14 at 11:22
-
Attempted to load class "SerializerBuilder" from namespace "Acme\MyBundle\Controller\JMS\Serializer" – madberdin Sep 08 '14 at 11:42
-
I see you've accapted the answer, thanks! So I hope you were successful in the end? – lxg Sep 08 '14 at 12:08
-
Yes) But the last question. This class makes value true for tinyINT fields from DB, how solve it? – madberdin Sep 08 '14 at 12:14
-
-
It looks like your type on these fields is boolean. However, if you're getting an unexpected result from the serializer, you should possibly discuss this in a new SO question (don't forget to post the relevant part of your schema/annotations). – lxg Sep 08 '14 at 12:21