mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
DDC-1776: Abstract class marked as @Entity in a single table inheritance is wrongly hydrated #2232
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 @doctrinebot on GitHub (Apr 14, 2012).
Originally assigned to: @Ocramius on GitHub.
Jira issue originally created by user benjamin:
I have the following entity hierarchy:
Although ProspectEvent is an abstract class, I need to mark it as @Entity, so that I can query for any type of ProspectEvent:
So far, so good.
The problem happens when I query the root class:
The query works fine, but the returned ProspectEvent entities have a NULL $prospect property.
I noticed that in that case, the DocBlock for $prospect is totally ignored (a wrong @JoinColumn name for example, doesn't throw an exception).
The workaround to make it work, is to add a "fake" entry to the discriminator map for the abstract class:
Even though there is no such "prospectEvent" discriminator entry possible, as the class is abstract.
Final point, if I remove the @Entity DocBlock on ProspectEvent, $prospect is hydrated correctly, but then it is not possible to issue a DQL query on ProspectEvent anymore.
@doctrinebot commented on GitHub (Nov 29, 2012):
Comment created by benjamin:
Any feedback?
@doctrinebot commented on GitHub (Jan 23, 2013):
Comment created by @ocramius:
[~benjamin] I'd say this has to be fixed in validation. What if we enforce the fake discriminator map? The "workaround" you suggested is actually what you should write with correct mappings.
@doctrinebot commented on GitHub (Feb 9, 2013):
Comment created by @ocramius:
Yes, this has to be reported in validation somehow. The "workaround" is not actually a workaround, but what is expected to be written by the developer.
@doctrinebot commented on GitHub (Feb 10, 2013):
Comment created by benjamin:
Ok, I found this weird because it's impossible to store a
ProspectEventin the database (as it'sabstract), so adding a discriminator entry for it looks weird to me.I would then suggest one of these two approaches:
abstract(this would be my preference).** Or, document this behavior, and* throw a proper exception if we try to persist / query such a hierarchy of classes.
But leaving the error unreported, and instantiating incomplete objects, is a real danger!
@doctrinebot commented on GitHub (May 1, 2013):
Comment created by @beberlei:
We fixed errors of this kind a while ago
@doctrinebot commented on GitHub (Sep 8, 2013):
Issue was closed with resolution "Duplicate"
@gdelprete-tl commented on GitHub (Dec 8, 2016):
I incurred in this bug yesterday.
In my case, I have some abstract
@Entityclasses with simple properties (non-association ones) in my hierarchy. The schema and persisting data works correctly, but when querying all elements by using the parent, objects of child classes gets loaded but properties in the abstract@Entitys are set to null (even if the row in the database has correct values). To fix this, I need to state discriminator values for abstract@Entitys too.@Ocramius commented on GitHub (Dec 8, 2016):
@gdelprete-tl that's not sufficient to analyze the problem: I suggest creating a test case as per examples in
d3f6c5ec70/tests/Doctrine/Tests/ORM/Functional/Ticket@BenMorel commented on GitHub (Aug 29, 2018):
I thought this had been fixed, but I had the same problem again today, on another project, using Doctrine 2.6.2. I have to add a fake entry (that can not exist in the db) to the discriminator map.
Hiearchy:
abstractUserActivity ($id,$user)abstractBookActivity ($book)$rating,$contents)If I select entities from the root
UserActivitywith DQL:Then
BookActivity::$bookis NULL ☹️Here is the fix that gets the entity properly hydrated:
I stand by my comment from 2013 that it does not make sense to have to add a discriminator entry for a value that cannot exist in the DB. Plus, why would I need to add a fake entry for the abstract
BookActivity, but not for the abstractUserActivityroot class, which is properly hydrated?Anyway, would this (inconsistent) behaviour be expected (I still can't find the relevant documentation, years later), Doctrine partially hydrating an object without either throwing an exception, or raising an error when validating the schema from the command-line tool, is definitely a bug.
Is it worth creating a failing test case to hopefully fix this issue in Doctrine 2.6, or are you guys busy on version 3 (that may be radically different?) and this would be a waste of time? Cheers 👍
@lcobucci commented on GitHub (Nov 20, 2018):
@BenMorel a failing test case is always helpful 😄
@jrcii commented on GitHub (Jan 13, 2023):
Doctrine's handling of abstract entity classes is incorrect. This comment was spot on https://github.com/doctrine/orm/issues/2428#issuecomment-162358894 The issue should not have been dismissed and closed without addressing the valid points raised. The abstract parent shouldn't be required as a discriminator entry.
Having the issue for over 10 years is one thing, doing so because we're pretending it's not a problem is another. I get the desire to reduce open issues but c'mon now.
@mpdude commented on GitHub (Jan 16, 2023):
I think #10411 could fill in the abstract intermediate classes that are needed for internal reasons, but do not make sense from a user's POV