[DDC-2225] Discriminator column is not considered correctly for class table inheritance Created: 07/Jan/13  Updated: 12/Feb/13  Resolved: 12/Feb/13

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: Git Master
Fix Version/s: None
Security Level: All

Type: Bug Priority: Minor
Reporter: Per Bernhardt Assignee: Benjamin Eberlei
Resolution: Won't Fix Votes: 0
Labels: None


Imagine having a class table inheritance for Employee and Person:

class Person {} => is mapped to table "person"
class Employee extends Person {} is mapped to table "person_employee"

When selecting "SELECT e FROM Employee" the SqlWalker will not generate a WHERE condition for a correct discriminator column value (e.g. "employee"). The ORM seems to trust on the generated INNER JOIN (person.id = person_employee.id).

Of course the table person_employee should only contain rows for Employee ojects. But if you have a discriminator value "person" for a row matching an Employee row, the repository will return a Person object although we selected from Employee.

A simple fix is to add the correct DQL expression:

Is there any reason, why the discriminator column is only considered for single table inheritance explicitly (see https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Query/SqlWalker.php#L426)?

Comment by Alexander [ 09/Feb/13 ]

Can you provide more information on your usecase and where this leads to wrong results? Unless you're altering the discriminator column values yourself this should be no problem?

Comment by Marco Pivetta [ 09/Feb/13 ]

JTI assumes that your rows in table `person_employee` are part of instances of `Employee`.

If you manually altered the discriminator column, that cannot be helped, since the ORM does not support casting.

Comment by Per Bernhardt [ 12/Feb/13 ]

Yes, the discriminator column is "manually" altered by our CMS. In other words, our CMS does not care to delete rows in any subtable, it only changes the discriminator column. So a valid solution for this situation is to change that, of course.

But why does the ORM check the discriminator column only for single table inheritance? In other words: Why do we need to have a discriminator column for class table inheritance, if the ORM does not rely on it?

Comment by Marco Pivetta [ 12/Feb/13 ]

It is used during the hydration step to decide what type your entity is as far as I know, even if all values in the joined tables are NULL.

Comment by Per Bernhardt [ 12/Feb/13 ]

Do you think it would break things when checking the discriminator column for class table inheritance in https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Query/SqlWalker.php#L426?

Comment by Marco Pivetta [ 12/Feb/13 ]

That check is not needed in JTI... And anyway, your logic wouldn't work if your CMS does an upcast somewhere.

Generated at Mon Nov 30 17:43:39 EST 2015 using JIRA 6.4.10#64025-sha1:5b8b74079161cd76a20ab66dda52747ee6701bd6.