DDC-2841: Preload data for association #3547

Closed
opened 2026-01-22 14:22:03 +01:00 by admin · 3 comments
Owner

Originally created by @doctrinebot on GitHub (Dec 6, 2013).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user sparrowek:

Consider two classes User and Address with one-to-many association.
I would like to preload addresses for given user object so that it will be accessible via getter method like this:

  1. $user = $userRepository->find($userId);
  2. load some of the addresses but not all
  3. $user->getAddresses() should return those loaded in step 2

Currently the only solution for step 2 I found working is to write a DQL like this:
SELECT u, a
FROM User
INNER JOIN u.addresses a
WHERE u = :user AND a.foo = 1

When the data is hydrated it is assigned to the user object and thus accessible with $user->getAddresses (which normally would return all the addresess not only those with foo set)

The only problem is that the query in step 2 unnecessarily fetches all user data that was already fetched in step 1

Originally created by @doctrinebot on GitHub (Dec 6, 2013). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user sparrowek: Consider two classes User and Address with one-to-many association. I would like to preload addresses for given user object so that it will be accessible via getter method like this: 1. $user = $userRepository->find($userId); 2. load some of the addresses but not all 3. $user->getAddresses() should return those loaded in step 2 Currently the only solution for step 2 I found working is to write a DQL like this: SELECT u, a FROM User INNER JOIN u.addresses a WHERE u = :user AND a.foo = 1 When the data is hydrated it is assigned to the user object and thus accessible with $user->getAddresses (which normally would return all the addresess not only those with foo set) The only problem is that the query in step 2 unnecessarily fetches all user data that was already fetched in step 1
admin added the New FeatureWon't Fix labels 2026-01-22 14:22:03 +01:00
admin closed this issue 2026-01-22 14:22:03 +01:00
Author
Owner

@mochja commented on GitHub (Oct 17, 2019):

Any update on this one? Can we achieve this without fetching the user data again?

@mochja commented on GitHub (Oct 17, 2019): Any update on this one? Can we achieve this without fetching the user data again?
Author
Owner

@Fedik commented on GitHub (Oct 18, 2019):

Much depend from type of association,
for M2O could be:
SELECT a FROM Address WHERE a.user = :user

@Fedik commented on GitHub (Oct 18, 2019): Much depend from type of association, for M2O could be: SELECT a FROM Address WHERE a.user = :user
Author
Owner

@Ocramius commented on GitHub (Oct 18, 2019):

I'd say that a fetch-joined DQL query is the correct approach, and that this feature is already implemented that way, as described in the OP.

Closing here as won't fix.

@Ocramius commented on GitHub (Oct 18, 2019): I'd say that a fetch-joined DQL query is the correct approach, and that this feature is already implemented that way, as described in the OP. Closing here as `won't fix`.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3547