JMSSerializerBundle
JMSSerializerBundle copied to clipboard
Disable JMSSerializer lazy-loading
Hello, I want to use JMSSerializer to serialize my entities to JSON. I write all my queries using Doctrine's DQL, to load exactly the relationships/columns I need to send to the user. But when I pass the entity I loaded with DQL to JMSSerializer, it lazy-load all the relationships/columns I didn't include in my DQL query. Is it possible to disable this behaviour somehow ? I simply want my DQL being transformed into JSON.
It is possible but it requires a lot of work. You can use the doctrine query AST to dynamicly set the ClassMetadata/PropertyMetadata
. I don't test it but theoretical it is possible, because you get all informations about the DQL from doctrine and you can set the ClassMetadata dynamicly through your own ClassMetadataFactory
. But it isn't a easy task.
:+1:
What about this solution? https://github.com/alcalyn/serializer-doctrine-proxies
@rvadym I really like what @alcalyn did in https://github.com/alcalyn/serializer-doctrine-proxies
unfortunately the solution is really specific and at the moment "incomplete"... as example:
- all doctrine entities with be serialized with the new handler (serializing only the id field), there is no way to choose the serialization strategy.
- it works only if the id field is called
id
, does not work if the filed has different names. - it does not work with composite keys (entities having more than one field as identifier)
Because of this, is not backward compatible with the jms serializer.
I really see a value in it, and probably can help to increase performances a lot. If somebody is interested to work on a similar solution, im really keen to help
It is possible but it requires a lot of work
That was a mess up to finally find this "middle" solution to make it work without running 1000+ sql queries with a leftJoin, and not having a 5Mb json response. And in a counterpart, using normal DQL, without thinking how to avoid over lazy loading by serializer.
If you could find a more generic Doctrine entities serializer, I'll be happy to test it !
A possible api for the feature can be:
class User
{
/**
* @var Post
* @Type("DoctrineIdentifiers<'Post, 'id', 'region'>")
*/
protected $post;
}
where:
-
DoctrineIdentifiers
is just a trigger for an event listener -
Post
is the class name of$post
(equivalent to@Type("Post")
) - 'id' and 'region' are the field names (in this case composite primary key...)
I need it too, unfortunately it is causing a very slow response to my API's. Have someone found a clear solution to this? 🙏