DDC-2849: Multiple selects of same entity with different conditions on associations return the same set of associated entities #3556

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

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

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user koubas:

When I execute multiple selects of the same entity, like these (pseudocode):

result1 = [ SELECT d,e FROM Department d LEFT JOIN d.employees e WITH e.type="manager" WHERE d.id=1 ]

result2 = [ SELECT d,e FROM Department d LEFT JOIN d.employees e WITH e.type="worker" WHERE d.id=1 ]

the resulting tree of entities could contain unexpected entities in association fields.

Because Doctrine's entity manager already has instance of Department entity with id 1, after the first select, the second result is the same instance. That is ok and it is also mentioned in docs. However, because of this, the "employees" association of that entity is populated with the same set of Employee entities (managers) in result1 and result2, even though the programmer would expect the association in second result to be populated with worker employees (i.e. she doesn't know somebody called the first query somewhere else in the app).

Is this a bug, or it's well known behavior and there is some best practice to overcome it?

Originally created by @doctrinebot on GitHub (Dec 11, 2013). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user koubas: When I execute multiple selects of the same entity, like these (pseudocode): result1 = [ SELECT d,e FROM Department d LEFT JOIN d.employees e WITH e.type="manager" WHERE d.id=1 ] result2 = [ SELECT d,e FROM Department d LEFT JOIN d.employees e WITH e.type="worker" WHERE d.id=1 ] the resulting tree of entities could contain unexpected entities in association fields. Because Doctrine's entity manager already has instance of Department entity with id 1, after the first select, the second result is the same instance. That is ok and it is also mentioned in docs. However, because of this, the "employees" association of that entity is populated with the same set of Employee entities (managers) in result1 and result2, even though the programmer would expect the association in second result to be populated with worker employees (i.e. she doesn't know somebody called the first query somewhere else in the app). Is this a bug, or it's well known behavior and there is some best practice to overcome it?
admin added the Bug label 2026-01-22 14:22:26 +01:00
admin closed this issue 2026-01-22 14:22:28 +01:00
Author
Owner

@doctrinebot commented on GitHub (Dec 13, 2013):

Comment created by @beberlei:

This is a known issue that cannot be fixed. Its actually why using WITH is not recommended and you should instead use an EXTRA LAZY collection and the Criteria API to filter for subsets.

@doctrinebot commented on GitHub (Dec 13, 2013): Comment created by @beberlei: This is a known issue that cannot be fixed. Its actually why using `WITH` is not recommended and you should instead use an EXTRA LAZY collection and the Criteria API to filter for subsets.
Author
Owner

@doctrinebot commented on GitHub (Dec 13, 2013):

Issue was closed with resolution "Can't Fix"

@doctrinebot commented on GitHub (Dec 13, 2013): Issue was closed with resolution "Can't Fix"
Author
Owner

@doctrinebot commented on GitHub (Dec 16, 2013):

Comment created by koubas:

It's not an issue only when using 'WITH', it happens even when I modify that queries: '... LEFT JOIN d.employees e WHERE e.type="manager" AND d.id=1'. Maybe the worst effect is not that I can't do more than one query with differently filtered association, but that I can't predict somebody did that before me somewhere else in code and I just get wrong result. Shouldn't it throw an exception, instead of silently returning wrong result?

@doctrinebot commented on GitHub (Dec 16, 2013): Comment created by koubas: It's not an issue only when using 'WITH', it happens even when I modify that queries: '... LEFT JOIN d.employees e WHERE e.type="manager" AND d.id=1'. Maybe the worst effect is not that I can't do more than one query with differently filtered association, but that I can't predict somebody did that before me somewhere else in code and I just get wrong result. Shouldn't it throw an exception, instead of silently returning wrong result?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3556