DDC-2590: Class inheritance - left join between child and parent entities #3253

Open
opened 2026-01-22 14:16:47 +01:00 by admin · 1 comment
Owner

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

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user darthcz:

The piece of code given under creates wrong sql to me.

Module is parent entity for BusinessModule entity. Category is joined with BusinessModule.

Module entity is only left joined to its child entity and that's the problem because it contains a field "name" used for filtering. So even if there is no module having the name, categories are still included.

I need the parent entity to be inner joined to child entity not left joined.

File doctrine2/lib/Doctrine/ORM/Query/SqlWalker.php line 353:

// If this is a joined association we must use left joins to preserve the correct result.
$sql .= isset($this->queryComponents[$dqlAlias]['relation']) ? ' LEFT ' : ' INNER ';
$qb->select('c')
->from('Category', 'c')
->join('c.module', 'm', 'WITH', 'm.name = :moduleName')
->setParameter('moduleName', $moduleName);
SELECT c0*.category_id AS category_id0, c0_.title AS title1, c0_.h1 AS h12, c0*.alias AS alias3,
c0*.insertion_fee AS insertion_fee4, c0_.description AS description5, c0_.parent_category*id AS
parent*category_id6, c0_.module_id AS module*id7 
FROM category c0_ 
INNER JOIN business*module b1_ ON c0_.module_id = b1_.module*id 
LEFT JOIN module m2* ON b1_.module_id = m2_.module_id AND (m2*.name = ?)
Originally created by @doctrinebot on GitHub (Aug 6, 2013). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user darthcz: The piece of code given under creates wrong sql to me. Module is parent entity for BusinessModule entity. Category is joined with BusinessModule. Module entity is only left joined to its child entity and that's the problem because it contains a field "name" used for filtering. So even if there is no module having the name, categories are still included. I need the parent entity to be inner joined to child entity not left joined. File `doctrine2/lib/Doctrine/ORM/Query/SqlWalker.php` line 353: ``` // If this is a joined association we must use left joins to preserve the correct result. $sql .= isset($this->queryComponents[$dqlAlias]['relation']) ? ' LEFT ' : ' INNER '; ``` ``` $qb->select('c') ->from('Category', 'c') ->join('c.module', 'm', 'WITH', 'm.name = :moduleName') ->setParameter('moduleName', $moduleName); ``` ``` SELECT c0*.category_id AS category_id0, c0_.title AS title1, c0_.h1 AS h12, c0*.alias AS alias3, c0*.insertion_fee AS insertion_fee4, c0_.description AS description5, c0_.parent_category*id AS parent*category_id6, c0_.module_id AS module*id7 FROM category c0_ INNER JOIN business*module b1_ ON c0_.module_id = b1_.module*id LEFT JOIN module m2* ON b1_.module_id = m2_.module_id AND (m2*.name = ?) ```
admin added the Bug label 2026-01-22 14:16:47 +01:00
Author
Owner

@doctrinebot commented on GitHub (Nov 6, 2013):

Comment created by maryo:

I am experiencing the same issue. The workarround could be to use LEFT JOIN with IS NOT NULL condition... But it also doesnt work after this commit
d9c1782a4f (i had to revert to dev-master#13c1efb240dd0af25ad0abe230df98ec895892c7)

@doctrinebot commented on GitHub (Nov 6, 2013): Comment created by maryo: I am experiencing the same issue. The workarround could be to use LEFT JOIN with IS NOT NULL condition... But it also doesnt work after this commit https://github.com/doctrine/doctrine2/commit/d9c1782a4f6d46f66e9deb2c375830f9192d4482 (i had to revert to dev-master#13c1efb240dd0af25ad0abe230df98ec895892c7)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3253