I have an Entity coming from a Doctrine repository, that I want to serialize and then return back. The Entity has of course several relations, those relations have also some realtions, and so on. Every relation that is needed, is already populated, BUT NOT ALL.
The properties of the Entities already have some serialization groups.
public function getListAction(SerializerInterface $serializer)
{
// ... Getting data from database with Doctrine
$entities = $this->someRepository->findByConditions(/* ... */);
$serializer->serialize($entities, 'json');
}
In the profiler I see that it starts to fill up the rest of the relations. Starts to execute database queries to fill up all the missing properties. Then eventually the query times out.
Question: Is there a way to tell the serializer, to only serialize the given object, but do not fill up the missing relations? In other words, disable lazy loading?
Adding a new serialization group would be also not good, because some Entity has a reference to itself $parent
. So it'll again end up in executing queries.
The Serializer is not able to see whether a call will trigger the lazy loading in Doctrine. You probably could write your own Normalizer that looks for classes from Doctrine's Proxy-namespace, but I imagine that will probably just becoome a hot mess if it will solve your problem in the first place.
The best solution that I can think of is tackling the issue at the ORM-level, as that is what is causing you problems.
For example you could create a custom find-method in a repository using DQL or QueryBuilder, which will fetch all necessary data including potentially joining in related objects you want to output and excluding any fields you don't want. Then I would hydrate the result (see Doctrine Docs: Hydration) using a different Hydrator. You could try the SimpleObjectHydrator provided by Doctrine. As far as I know, this Hydrator will not use Proxies for Lazy Loading and instead will load as much as it can eagerly and it should still give you your original Entity, but the eager loading might not be what you want. In that case you can use it as a reference for your own Hydrator. Writing custom hydrators might be a huge pain and you could instead opt for array output and just forego the object hydration entirely. In that case you are sure to lose the annotations in your entity, which you would then need to address in your Serializer logic.
You could also look at Native Queries (i.e. SQL) and then custom Result Set Mapping as an alternative: see Doctrine Docs: ResultSetMappingBuilder