Doctrine does not have a method to convert entities to JSON #6814

Closed
opened 2026-01-22 15:39:16 +01:00 by admin · 5 comments
Owner

Originally created by @ghost on GitHub (Sep 1, 2021).

Feature Request

Q A
New Feature yes
RFC yes/no
BC Break yes/no

Summary

This feature is EXTREMELY basic and the whole doctrine ORM is basically useless if you are unable to convert entities (select query results, etc) into json for returning in an API. Either the feature doesn't exist, which is honestly unbelievable, or there is a desperate need to add documentation for it, since not even stackoverflow requests have come to any reasonable conclusion in the past 10 years.

Originally created by @ghost on GitHub (Sep 1, 2021). ### Feature Request <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | New Feature | yes | RFC | yes/no | BC Break | yes/no #### Summary This feature is EXTREMELY basic and the whole doctrine ORM is basically useless if you are unable to convert entities (select query results, etc) into json for returning in an API. Either the feature doesn't exist, which is honestly unbelievable, or there is a desperate need to add documentation for it, since not even stackoverflow requests have come to any reasonable conclusion in the past 10 years.
admin closed this issue 2026-01-22 15:39:16 +01:00
Author
Owner

@derrabus commented on GitHub (Sep 1, 2021):

Thank you for your friendly request. The feature that you're looking for is called json_encode(), see https://www.php.net/json_encode

If you need more sophisticated serialization solutions, there's Symfony Serializer, JMS Serializer and other high quality open source libraries. I don't think that Doctrine has to invent its own solution.

@derrabus commented on GitHub (Sep 1, 2021): Thank you for your friendly request. The feature that you're looking for is called `json_encode()`, see https://www.php.net/json_encode If you need more sophisticated serialization solutions, there's Symfony Serializer, JMS Serializer and other high quality open source libraries. I don't think that Doctrine has to invent its own solution.
Author
Owner

@ghost commented on GitHub (Sep 1, 2021):

@derrabus I don't see anywhere stated that, say, the Symfony serializer or JMS Serializer provides support for Doctrine. Notice that serialization of Doctrine Entities might lead to infinite recursion because of associations. Every ORM I have ever used throughout 10 years of web development career provide serialization methods because of this very issue. It is important to have a serializer that provides support for Doctrine, not merely a generic serializer. Or are Doctrine entities, query results, etc, with associations ready to be blindly serialized?

@ghost commented on GitHub (Sep 1, 2021): @derrabus I don't see anywhere stated that, say, the Symfony serializer or JMS Serializer provides support for Doctrine. Notice that serialization of Doctrine Entities might lead to infinite recursion because of associations. Every ORM I have ever used throughout 10 years of web development career provide serialization methods because of this very issue. It is important to have a serializer that provides support for Doctrine, not merely a generic serializer. Or are Doctrine entities, query results, etc, with associations ready to be blindly serialized?
Author
Owner

@derrabus commented on GitHub (Sep 1, 2021):

The solutions I've provided you with are used quite commonly for serializing entity models hydrated with Doctrine ORM. Give them a try.

@derrabus commented on GitHub (Sep 1, 2021): The solutions I've provided you with are used quite commonly for serializing entity models hydrated with Doctrine ORM. Give them a try.
Author
Owner

@ghost commented on GitHub (Sep 1, 2021):

@derrabus To use the JMS Serializer Bundle in Symfony, I need to access the "$container" variable in Symfony controllers, which there is no documentation about how to access that variable. There is no minimal documentation about how to use the JMS Serializer Bundle. Please notice that json serialization is an EXTREMELY BASIC feature, and it is not minimally documented anywhere.

@ghost commented on GitHub (Sep 1, 2021): @derrabus To use the JMS Serializer Bundle in Symfony, I need to access the "$container" variable in Symfony controllers, which there is no documentation about how to access that variable. There is no minimal documentation about how to use the JMS Serializer Bundle. Please notice that json serialization is an EXTREMELY BASIC feature, and it is not minimally documented anywhere.
Author
Owner

@derrabus commented on GitHub (Sep 1, 2021):

As mentioned on symfony/symfony#42833, use Symfony's discussions for support on Symfony.

https://github.com/symfony/symfony/discussions

@derrabus commented on GitHub (Sep 1, 2021): As mentioned on symfony/symfony#42833, use Symfony's discussions for support on Symfony. https://github.com/symfony/symfony/discussions
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6814