doctrine / doctrine/orm

DDC-3697: WHERE conditions can get moved into JOIN conditions with JOINed Inheritance and non-association-JOINs

Open
#4,530 6 comments 0 reactions 1 assignee Claimed by @guilhermeblanco View on GitHub
Bug
Dominant language
PHP
Stars
10.2k
Forks
2.5k
Avg merge
1d 10h
Merged PRs (30d)
34

Description

Jira issue originally created by user MalteWunsch:

With the following entities:

```
/****
* @ORM\Entity()
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="discriminaor", type="integer")
* @ORM\DiscriminatorMap({"1" = "GeneralEntity", "2" = "SpecializedEntity"})
*/
class GeneralEntity {
/****
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
}

/****
* @ORM\Entity()
*/
class SpecializedEntity extends GeneralEntity {
}
```

you can create a DQL query (note we're JOINing freely here without traversing a defined association) like

```
SELECT g1.id
FROM GeneralEntity g1
JOIN GeneralEntity g2
WHERE g2.id = 1
```

This gets converted to the following SQL:

```
SELECT g0_.id AS id0
FROM GeneralEntity g0_
LEFT JOIN SpecializedEntity s1* ON g0_.id = s1*.id
INNER JOIN GeneralEntity g2_
LEFT JOIN SpecializedEntity s3* ON g2_.id = s3_.id AND (g2*.id = 1)
```

As you can see, the condition in the WHERE part (g2.id = 1) is no longer in a WHERE clause by it's own, but got moved to the LEFT JOIN of the table inheritance. In this simple special case it probably makes no difference, but in general it does and leads to wrong results.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.