Skip to content

Loading…

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

Closed
doctrinebot opened this Issue · 8 comments

2 participants

@doctrinebot

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.

@doctrinebot

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...

@doctrinebot

Comment created by @ocramius:

Duplicate of DDC-2235

@doctrinebot

Issue was closed with resolution "Duplicate"

@doctrinebot

Comment created by @doctrinebot:

A related Github Pull-Request [GH-656] was closed:
#656

@doctrinebot

Comment created by @doctrinebot:

A related Github Pull-Request [GH-758] was closed:
#758

@doctrinebot

Comment created by @doctrinebot:

A related Github Pull-Request [GH-656] was closed:
doctrine/dbal#656

@doctrinebot

Comment created by @doctrinebot:

A related Github Pull-Request [GH-758] was assigned:
doctrine/dbal#758

@doctrinebot

Comment created by @doctrinebot:

A related Github Pull-Request [GH-758] was closed:
doctrine/dbal#758

@Ocramius Ocramius was assigned by doctrinebot
@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.