Symfony 5 – DQL requests are using SELECT automatically on my User relationships (explosion in the number of requests)

in my Symfony 5 project, I have a User entity which has various attributes including relationships with the entities:

  • Avatar
  • Signature
  • SidebarOptions
  • BalanceConges
  • StripeCustomer

When I do this kind of DQL query to get company users ( There are 100 users in the company passed in parameter)

/**
 * Retourne les users appartenant à l'entreprise passée en paramètre.
 *
 * @return User[]
 */
public function findByEntreprise(Entreprise $entreprise, $enabled = true)
{
    return $this->createQueryBuilder('u')
        ->join("u.entreprise", "e")
        ->addSelect("e")
        ->where('e.id = :entreprise')
        ->andWhere('u.enabled = :enabled')
        ->setParameter('entreprise', $entreprise->getId())
        ->setParameter('enabled', $enabled)
        ->getQuery()
        ->getResult();
}

And that I return in my controller, an empty Twig view, I see that I have 527 requests in the profiler :

500 requests about my user relationships:

enter image description here

enter image description here

It does this to me for the 5 relationships of my User entity.

For each of the users (100 users), it will make a request to obtain the data related to the relationships of the user (those that I mentioned above). Which will make 5 requests per user. If I have 100 users, it will generate 500 requests for me, whereas I don’t want it to automatically hydrate relations if I don’t use them (which is confirmed since I tested by returning a completely empty view where I’m not doing anything)

It automatically hydrates this.

It’s weird. Especially since the Twig view that I render is completely empty. The 500 queries come from the DQL query that I make, and it automatically hydrates the 5 relationships of User.

I noticed that for these 500 requests to disappear, there were two ways:

Add LEFT JOIN on the querybuilder function :

/**
 * Retourne les users appartenant à l'entreprise passée en paramètre.
 *
 * @return User[]
 */
public function findByEntreprise(Entreprise $entreprise, $enabled = true)
{
    return $this->createQueryBuilder('u')
        ->join("u.entreprise", "e")
        ->leftJoin("u.avatar", "a")
        ->leftJoin("u.signature", "s")
        ->leftJoin("u.sidebarOptions", "so")
        ->leftJoin("u.soldeConges", "sc")
        ->leftJoin("u.stripeCustomer", "scus")
        ->addSelect("e, a, s, so, sc, scus")
        ->where('e.id = :entreprise')
        ->andWhere('u.enabled = :enabled')
        ->setParameter('entreprise', $entreprise->getId())
        ->setParameter('enabled', $enabled)
        ->getQuery()
        ->getResult();
}

Or just use :

return $this->findBy([
            "entreprise" => $entreprise,
            "enabled" => true
        ]);

The problem is that I have a lot of requests in my application that must appeal to my users, and each time I notice the same problem. Could someone explain to me what’s going on?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *