DDC-2497: When trying to join single-table inheritance classes in DQL or QueryBuilder, Doctrine creates incorrect SQL #3132

Closed
opened 2026-01-22 14:13:13 +01:00 by admin · 8 comments
Owner

Originally created by @doctrinebot on GitHub (Jun 10, 2013).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user baohx2000:

When writing a DQL query joining to inherited subclasses, Doctrine incorrectly injects the discriminator in the WHERE clause instead of in the JOIN, where it should be.
Example: 2 classes extend Person via JOINED table inheritance: EmployedPerson and UnemployedPerson. I want to create a query where I pull separate class-specific properties form each class:

SELECT p FROM Person p
LEFT JOIN EmployedPerson ep WITH ep=p
LEFT JOIN UnemployedPerson up WITH up=p

The SQL from this will look something like:
SELECT .... FROM person p
LEFT JOIN person ep ON(ep.id=p.id)
LEFT JOIN person up ON(up.id=p.id)
WHERE ep.discrim IN('employed') AND up.discrim IN('unemployed')

The problem is this query will produce no results if one of the subclasses have no results.

The fix would be putting the discriminator in the JOIN so the sql would be:

SELECT .... FROM person p
LEFT JOIN person ep ON(ep.id=p.id AND ep.discrim IN('employed'))
LEFT JOIN person up ON(up.id=p.id AND up.discrim IN('unemployed'))

I will try to create a unit test based on the current ones and upload it.

Originally created by @doctrinebot on GitHub (Jun 10, 2013). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user baohx2000: When writing a DQL query joining to inherited subclasses, Doctrine incorrectly injects the discriminator in the WHERE clause instead of in the JOIN, where it should be. Example: 2 classes extend Person via JOINED table inheritance: EmployedPerson and UnemployedPerson. I want to create a query where I pull separate class-specific properties form each class: SELECT p FROM Person p LEFT JOIN EmployedPerson ep WITH ep=p LEFT JOIN UnemployedPerson up WITH up=p The SQL from this will look something like: SELECT .... FROM person p LEFT JOIN person ep ON(ep.id=p.id) LEFT JOIN person up ON(up.id=p.id) WHERE ep.discrim IN('employed') AND up.discrim IN('unemployed') The problem is this query will produce no results if one of the subclasses have no results. The fix would be putting the discriminator in the JOIN so the sql would be: SELECT .... FROM person p LEFT JOIN person ep ON(ep.id=p.id AND ep.discrim IN('employed')) LEFT JOIN person up ON(up.id=p.id AND up.discrim IN('unemployed')) I will try to create a unit test based on the current ones and upload it.
admin added the Bug label 2026-01-22 14:13:13 +01:00
admin closed this issue 2026-01-22 14:13:14 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jun 10, 2013):

Comment created by baohx2000:

Sorry, this is a duplicate of http://www.doctrine-project.org/jira/browse/DDC-2235
I don't see where to mark as dupe...

@doctrinebot commented on GitHub (Jun 10, 2013): Comment created by baohx2000: Sorry, this is a duplicate of http://www.doctrine-project.org/jira/browse/[DDC-2235](http://www.doctrine-project.org/jira/browse/DDC-2235) I don't see where to mark as dupe...
Author
Owner

@doctrinebot commented on GitHub (Jun 11, 2013):

Comment created by @ocramius:

Duplicate of DDC-2235

@doctrinebot commented on GitHub (Jun 11, 2013): Comment created by @ocramius: Duplicate of [DDC-2235](http://www.doctrine-project.org/jira/browse/DDC-2235)
Author
Owner

@doctrinebot commented on GitHub (Jun 11, 2013):

Issue was closed with resolution "Duplicate"

@doctrinebot commented on GitHub (Jun 11, 2013): Issue was closed with resolution "Duplicate"
Author
Owner

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

Comment created by @doctrinebot:

A related Github Pull-Request [GH-656] was closed:
https://github.com/doctrine/doctrine2/pull/656

@doctrinebot commented on GitHub (Aug 16, 2013): Comment created by @doctrinebot: A related Github Pull-Request [GH-656] was closed: https://github.com/doctrine/doctrine2/pull/656
Author
Owner

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

Comment created by @doctrinebot:

A related Github Pull-Request [GH-758] was closed:
https://github.com/doctrine/doctrine2/pull/758

@doctrinebot commented on GitHub (Aug 16, 2013): Comment created by @doctrinebot: A related Github Pull-Request [GH-758] was closed: https://github.com/doctrine/doctrine2/pull/758
Author
Owner

@doctrinebot commented on GitHub (Aug 9, 2014):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-656] was closed:
https://github.com/doctrine/dbal/pull/656

@doctrinebot commented on GitHub (Aug 9, 2014): Comment created by @doctrinebot: A related Github Pull-Request [GH-656] was closed: https://github.com/doctrine/dbal/pull/656
Author
Owner

@doctrinebot commented on GitHub (Dec 26, 2014):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-758] was assigned:
https://github.com/doctrine/dbal/pull/758

@doctrinebot commented on GitHub (Dec 26, 2014): Comment created by @doctrinebot: A related Github Pull-Request [GH-758] was assigned: https://github.com/doctrine/dbal/pull/758
Author
Owner

@doctrinebot commented on GitHub (Dec 27, 2014):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-758] was closed:
https://github.com/doctrine/dbal/pull/758

@doctrinebot commented on GitHub (Dec 27, 2014): Comment created by @doctrinebot: A related Github Pull-Request [GH-758] was closed: https://github.com/doctrine/dbal/pull/758
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3132