Nested setFetchMode #7322

Open
opened 2026-01-22 15:49:54 +01:00 by admin · 2 comments
Owner

Originally created by @gharlan on GitHub (Feb 13, 2024).

Originally assigned to: @beberlei on GitHub.

Feature Request

Q A
New Feature yes
RFC no
BC Break no

Summary

At the moment I'm using partial queries for fetching nested entities:

$qb = $this->createQueryBuilder('mail');
// ...
$mails = $qb->getQuery()->getResult();

$qb = $this->createQueryBuilder('mail');
$qb->select("PARTIAL mail.{id}, recipient, user");
$qb->andWhere("mail IN (:mails)")->setParameter('mails', $mails);
$qb->leftJoin('mail.recipients', 'recipient');
$qb->leftJoin('recipient.user', 'user');

Now I'm trying to convert this to setFetchMode (https://github.com/doctrine/orm/pull/8391) because partial queries are removed in v3. I've tried this:

$qb = $this->createQueryBuilder('mail');
// ...
$query = $qb->getQuery();

$query->setFetchMode(Mail::class, 'recipients', ClassMetadata::FETCH_EAGER); // this works great
$query->setFetchMode(MailRecipient::class, 'user', ClassMetadata::FETCH_EAGER); // this does not work

$mails = $query->getResult();

The second setFetchMode call does not have any effect. It would be great if setFetchMode would work for nested relations.

Or is there any other (good) replacement for my partial query?

Originally created by @gharlan on GitHub (Feb 13, 2024). Originally assigned to: @beberlei on GitHub. ### Feature Request <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | New Feature | yes | RFC | no | BC Break | no #### Summary <!-- Provide a summary of the feature you would like to see implemented. --> At the moment I'm using partial queries for fetching nested entities: ```php $qb = $this->createQueryBuilder('mail'); // ... $mails = $qb->getQuery()->getResult(); $qb = $this->createQueryBuilder('mail'); $qb->select("PARTIAL mail.{id}, recipient, user"); $qb->andWhere("mail IN (:mails)")->setParameter('mails', $mails); $qb->leftJoin('mail.recipients', 'recipient'); $qb->leftJoin('recipient.user', 'user'); ``` Now I'm trying to convert this to `setFetchMode` (https://github.com/doctrine/orm/pull/8391) because partial queries are removed in v3. I've tried this: ```php $qb = $this->createQueryBuilder('mail'); // ... $query = $qb->getQuery(); $query->setFetchMode(Mail::class, 'recipients', ClassMetadata::FETCH_EAGER); // this works great $query->setFetchMode(MailRecipient::class, 'user', ClassMetadata::FETCH_EAGER); // this does not work $mails = $query->getResult(); ``` The second `setFetchMode` call does not have any effect. It would be great if `setFetchMode` would work for nested relations. Or is there any other (good) replacement for my partial query?
Author
Owner

@beberlei commented on GitHub (Aug 20, 2024):

@gharlan I am assigning this to myself to take a look what we can do as soon as I have time, probably around the Doctrine Hackathon in October.

@beberlei commented on GitHub (Aug 20, 2024): @gharlan I am assigning this to myself to take a look what we can do as soon as I have time, probably around the Doctrine Hackathon in October.
Author
Owner

@JanTvrdik commented on GitHub (Oct 15, 2024):

@gharlan We've recently published a library to work around the current limitations of setFetchMode and/or the need to write custom partial queries (which are now working again, yeah!) that should work for your use case.

This is how fetching nested entitiets from your example would look with the EntityPreloader.

$mails = $query->getResult();

$preloader = new EntityPreloader($entityManager);
$recipients = $preloader->preload($mails, 'recipients');
$preloader->preload($recipients, 'user');
@JanTvrdik commented on GitHub (Oct 15, 2024): @gharlan We've recently published a library to work around the current limitations of `setFetchMode` and/or the need to write custom partial queries (which are [now working again](https://github.com/doctrine/orm/releases/tag/3.3.0), yeah!) that should work for your use case. This is how fetching nested entitiets from your example would look with the [EntityPreloader](https://github.com/shipmonk-rnd/doctrine-entity-preloader). ```php $mails = $query->getResult(); $preloader = new EntityPreloader($entityManager); $recipients = $preloader->preload($mails, 'recipients'); $preloader->preload($recipients, 'user'); ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7322