Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-2225

Discriminator column is not considered correctly for class table inheritance

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Won't Fix
    • Affects Version/s: Git Master
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None

      Description

      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:
      SELECT e FROM Employee WHERE e INSTANCE OF Employee

      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)?

        Activity

        Per Bernhardt created issue -
        Hide
        Alexander added a comment -

        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?

        Show
        Alexander added a comment - 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?
        Alexander made changes -
        Field Original Value New Value
        Status Open [ 1 ] Awaiting Feedback [ 10000 ]
        Hide
        Marco Pivetta added a comment -

        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.

        Show
        Marco Pivetta added a comment - 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.
        Hide
        Per Bernhardt added a comment -

        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?

        Show
        Per Bernhardt added a comment - 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?
        Hide
        Marco Pivetta added a comment -

        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.

        Show
        Marco Pivetta added a comment - 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.
        Hide
        Per Bernhardt added a comment -

        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?

        Show
        Per Bernhardt added a comment - 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?
        Hide
        Marco Pivetta added a comment -

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

        Show
        Marco Pivetta added a comment - That check is not needed in JTI... And anyway, your logic wouldn't work if your CMS does an upcast somewhere.
        Per Bernhardt made changes -
        Status Awaiting Feedback [ 10000 ] In Progress [ 3 ]
        Per Bernhardt made changes -
        Status In Progress [ 3 ] Resolved [ 5 ]
        Resolution Won't Fix [ 2 ]

        This list may be incomplete, as errors occurred whilst retrieving source from linked applications:

        • Request to http://www.doctrine-project.org/fisheye/ failed: Error in remote call to 'FishEye 0 (http://www.doctrine-project.org/fisheye/)' (http://www.doctrine-project.org/fisheye) [AbstractRestCommand{path='/rest-service-fe/search-v1/crossRepositoryQuery', params={query=DDC-2225, expand=changesets[0:20].revisions[0:29],reviews}, methodType=GET}] : Received status code 503 (Service Temporarily Unavailable)

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Per Bernhardt
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: