mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Doctrine 2.6 : Inheritance (instance of) does not work #5844
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @christophe-mailfert on GitHub (Jan 11, 2018).
Originally assigned to: @lcobucci on GitHub.
Hi guys,
Doctrine ORM version : 2.6.0
I'm currently migrating my project from symfony3 to symfony4 flex and my unit tests are failing. The reason:
Instance of does not generate correct sql request
Generated sql
Instead of
Code in repository:
See you :)
@Majkl578 commented on GitHub (Jan 11, 2018):
Can you please also share your entities/mapping to illustrate the inheritance tree? ideally as a failing test case that reproduces the bug? It'd be really helpful in order to identify the issue.
You can find test examples on https://github.com/doctrine/doctrine2/tree/v2.6.0/tests/Doctrine/Tests/ORM/Functional/Ticket.
Thanks.
@christophe-mailfert commented on GitHub (Jan 11, 2018):
Sure here is my entities:
I will add a test as soon as possible :).
@lcobucci commented on GitHub (Jan 15, 2018):
@christophe-mailfert the SQL is being generated correctly, what's happening is that you're not using inheritance correctly.
Objects with type
BusStopare also an instance ofPoint, e.g.:Therefore if you're using
$builder->expr()->isInstanceOf('p', Point::class), the generated SQL will useinterestANDstopas filter.This was a bug in v2.5.x, which was fixed in v2.6 (#6392).
@christophe-mailfert commented on GitHub (Jan 15, 2018):
Hey Icobucci,
Thx for your response.
So with my current schema, it's not possible to handle inheritance ?
Should i transform Point to an Abstract class and create a child InterestPoint who extends AbstractPoint ? or there is another solution with current schema ?
Thx for advance.
@lcobucci commented on GitHub (Jan 15, 2018):
That's indeed a viable solution, and it won't require a redesign of your models.
@koseduhemak commented on GitHub (Jan 18, 2018):
The following does also not work in
2.6.0:Problem is that
$array = $this->entityManager->getRepository(Project::class)->findAll();does not return any entities of typeSubProject, only of typeProject, evenSubProjectis of typeProjectthough. However, there are manynullvalues (I think everySubProjectgets hydrated asnullvalue) in the resulting$array.The same code works with
2.5.14.@lcobucci commented on GitHub (Jan 18, 2018):
@koseduhemak your mapping seems invalid to me, since it doesn't follow any of the guidelines explained in http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html.
That might be the cause of not getting the correct data, however it's kind of scary that it used to work on
v2.5.14.@koseduhemak commented on GitHub (Jan 18, 2018):
@lcobucci Sorry, did some mistakes by copying and simplifying my example.
Corrected it in my previous answer (it was way more complex, but I tried to reduce complexity and make a shorter, easier to understand example).
I followed the guidelines here: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#single-table-inheritance
@lcobucci commented on GitHub (Jan 18, 2018):
@koseduhemak so you really don't have a
@DiscriminatorMap? Could you try to send a PR with a failing functional test with this scenario (targeting2.6)?@koseduhemak commented on GitHub (Jan 18, 2018):
@lcobucci nope, I do not explicitly specify a
@DiscriminatorMap. I thought (and according to the docs), we do not necessarily specify one: "If no discriminator map is provided, then the map is generated automatically. The automatically generated discriminator map contains the lowercase short name of each class as key." (Source: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#single-table-inheritance, last bullet).However, I tried to create a functional test reproducing my issue without luck. I tried
2.6but also2.5and in both cases I get an exceptionDoctrine\ORM\Mapping\MappingException: Entity 'Doctrine\Tests\Models\Project\SubProject' has to be part of the discriminator map of 'Doctrine\Tests\Models\Project\Project' to be properly mapped in the inheritance hierarchy. Alternatively you can make 'Doctrine\Tests\Models\Project\SubProject' an abstract class to avoid this exception from occurring..This leads to the question, why, in my own project, I am able to specify
SINGLE_TABLEinheritances without@DiscrimintatorMap.@insekticid commented on GitHub (Jan 3, 2019):
same problem here with doctrine/orm (v2.6.3) - using InheritanceType("JOINED")
Workaround:.
EDIT:
now I see, that this behavior is feature :(
There should be some option to override this behavior
@lcobucci commented on GitHub (Jan 5, 2019):
@insekticid the ORM should behave the same way as the language, there's no point in adding a flag to modify that behaviour.
In your case, I'd dare to say that you probably have a design issue when you say that a Page is an Article (that's the relation type you have when using inheritance in object oriented programming).
I'll close this as invalid since we don't have any failing test that proves the bug.
@insekticid commented on GitHub (Jan 17, 2019):
@lcobucci
EDIT:
found problem: ýou cannot use join WITH $expr, you have to use where...
Not working
Working