JMSSerializerBundle icon indicating copy to clipboard operation
JMSSerializerBundle copied to clipboard

Disable JMSSerializer lazy-loading

Open jgoux opened this issue 10 years ago • 7 comments

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.

jgoux avatar Sep 19 '14 13:09 jgoux

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.

Baachi avatar Apr 01 '15 11:04 Baachi

:+1:

bmeynell avatar Aug 04 '15 14:08 bmeynell

What about this solution? https://github.com/alcalyn/serializer-doctrine-proxies

rvadym avatar May 22 '17 18:05 rvadym

@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

goetas avatar May 23 '17 09:05 goetas

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 !

alcalyn avatar May 23 '17 09:05 alcalyn

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...)

goetas avatar May 23 '17 10:05 goetas

I need it too, unfortunately it is causing a very slow response to my API's. Have someone found a clear solution to this? 🙏

romeugodoi avatar Apr 06 '18 19:04 romeugodoi