Doctrine Projecthttp://www.doctrine-project.org/jira
This file is an XML representation of an issueen-us6.2.3626015-04-2014[DDC-2225] Discriminator column is not considered correctly for class table inheritancehttp://www.doctrine-project.org/jira/browse/DDC-2225
Doctrine 2 - ORM<p>Imagine having a class table inheritance for Employee and Person:</p>
<p>class Person {} =&gt; is mapped to table "person"<br/>
class Employee extends Person {} is mapped to table "person_employee"</p>
<p>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).</p>
<p>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.</p>
<p>A simple fix is to add the correct DQL expression:<br/>
SELECT e FROM Employee WHERE e INSTANCE OF Employee</p>
<p>Is there any reason, why the discriminator column is only considered for single table inheritance explicitly (see <a href="https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Query/SqlWalker.php#L426)?" class="external-link" rel="nofollow">https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Query/SqlWalker.php#L426)?</a></p>DDC-2225Discriminator column is not considered correctly for class table inheritanceBugMinorResolvedWon't FixAllBenjamin EberleiPer BernhardtMon, 7 Jan 2013 15:02:56 +0000Tue, 12 Feb 2013 10:45:29 +0000Tue, 12 Feb 2013 10:45:29 +0000Git MasterORM03<p>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?</p><p>JTI assumes that your rows in table `person_employee` are part of instances of `Employee`.</p>
<p>If you manually altered the discriminator column, that cannot be helped, since the ORM does not support casting.</p><p>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.</p>
<p>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?</p><p>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.</p><p>Do you think it would break things when checking the discriminator column for class table inheritance in <a href="https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Query/SqlWalker.php#L426?" class="external-link" rel="nofollow">https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Query/SqlWalker.php#L426?</a></p><p>That check is not needed in JTI... And anyway, your logic wouldn't work if your CMS does an upcast somewhere.</p>