DDC-812: DQL hydration of joined objects skipped when main instance already present in entity manager #999

Closed
opened 2026-01-22 12:58:32 +01:00 by admin · 3 comments
Owner

Originally created by @doctrinebot on GitHub (Sep 21, 2010).

Jira issue originally created by user dennis.verspuij:

I do the following:

$author1 = $em->find('Author', 1);

$author1again = $em
  ->createQuery(
    'SELECT A, B '.
    'FROM Author A '.
    'INNER JOIN A.Books B '.
   'WHERE A.ID = 1'
  )
  ->getSingleResult();

the Books collection is not hydrated because the author instance was already
hydrated when populating $author1. This means when I call
count($author1->getBooks()) a third query is executed to fetch the Books again.

The result is even more problematic when adding a WITH clause to the
INNER JOIN, in that case one would expect the Books collection to only
contain the books matching matching that criteria.

The result is expected when commenting out the $author1 =... line, thus when
the author does not already exist in the entity manager.

I'm sorry I can't produce a test case right now.

Originally created by @doctrinebot on GitHub (Sep 21, 2010). Jira issue originally created by user dennis.verspuij: I do the following: ``` $author1 = $em->find('Author', 1); $author1again = $em ->createQuery( 'SELECT A, B '. 'FROM Author A '. 'INNER JOIN A.Books B '. 'WHERE A.ID = 1' ) ->getSingleResult(); ``` the Books collection is not hydrated because the author instance was already hydrated when populating $author1. This means when I call count($author1->getBooks()) a third query is executed to fetch the Books again. The result is even more problematic when adding a WITH clause to the INNER JOIN, in that case one would expect the Books collection to only contain the books matching matching that criteria. The result is expected when commenting out the $author1 =... line, thus when the author does not already exist in the entity manager. I'm sorry I can't produce a test case right now.
admin added the Bug label 2026-01-22 12:58:32 +01:00
admin closed this issue 2026-01-22 12:58:33 +01:00
Author
Owner

@doctrinebot commented on GitHub (Sep 21, 2010):

Comment created by @beberlei:

fix formatting

@doctrinebot commented on GitHub (Sep 21, 2010): Comment created by @beberlei: fix formatting
Author
Owner

@doctrinebot commented on GitHub (Oct 31, 2010):

Comment created by romanb:

Fixed in master. Note that your second case still "won't work" from your point of view. If the collection is already initialized it is not overridden. This is consistent with other behavior (what is in-memory is preserved). Fetch-joinining a collection and using WITH at the same time is not a nice thing to do since the meaning of the association is changed per-query. First $author->getBooks() means one thing (all books written by this author) and a few lines later something else (all books written by this author in the last 10 years or whatever). To get the desired result in your second case, that is, overriding the collection in-memory, set the REFRESH query hint. Note that this will change the meaning of Author#books though, as is always the case with fetch-join + WITH.

@doctrinebot commented on GitHub (Oct 31, 2010): Comment created by romanb: Fixed in master. Note that your second case still "won't work" from your point of view. If the collection is already initialized it is not overridden. This is consistent with other behavior (what is in-memory is preserved). Fetch-joinining a collection and using WITH at the same time is not a nice thing to do since the meaning of the association is changed per-query. First $author->getBooks() means one thing (all books written by this author) and a few lines later something else (all books written by this author in the last 10 years or whatever). To get the desired result in your second case, that is, overriding the collection in-memory, set the REFRESH query hint. Note that this will change the meaning of Author#books though, as is always the case with fetch-join + WITH.
Author
Owner

@doctrinebot commented on GitHub (Oct 31, 2010):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Oct 31, 2010): Issue was closed with resolution "Fixed"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#999