Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix single table inheritance with intermediate abstract class(es) #10643

Merged

Conversation

htto
Copy link

@htto htto commented Apr 20, 2023

We need to make sure to query the discriminator map only with contained classes, otherwise we end up trying to access it with e.g. abstract sub-classes of the current class, which are not to be contained in the discriminator map. This is caused by the introduction of auto-discovering of missing sub-classes in #10411 which also added abstract classes to ClassMetadata::subClasses.

Fixes #10625
Test based on PR #10411


foreach ($this->class->subClasses as $subclassName) {
$values[] = $this->conn->quote($discrValues[$subclassName]);
array_unshift($values, $this->conn->quote($this->class->discriminatorValue));
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A different approach would be extending of ClassMetadata(Info) with something in the lines of discriminatorSubClasses. This would cause more work and refactoring, though.

Copy link
Contributor

@mpdude mpdude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the author of #10411 I can approve this.

Before #10411, all subclasses had to be declared in the discriminator map, or you'd get strange errors elsewhere.

Since #10411, abstract entity classes may be omitted in the discriminator map, and they'd be filled into \Doctrine\ORM\Mapping\ClassMetadataInfo::$subClasses automatically. That, in turn, means there may be subClasses without corresponding discriminator map entries.

We cannot look at the discriminator map only, since it contains all classes from the inheritance tree. In this situation here, we are only looking for those at or below the current class, so we need to intersect both subClasses and the discriminatorMap.

@htto
Copy link
Author

htto commented Apr 25, 2023

I'm not quite sure, that all subclasses had to be in the discriminator map before #10411 when the documentation only included concrete implementations itself, e.g. in [1]. On the other hand the annotation documentation is a bit vague [2]

Its only argument is an array which defines which class should be saved under which name in the database.

Though this doesn't address abstract classes, it only refers to database backed entities, which IMHO shouldn't be abstract anyway.

[1] https://www.doctrine-project.org/projects/doctrine-orm/en/2.12/cookbook/decorator-pattern.html
[2] https://www.doctrine-project.org/projects/doctrine-orm/en/2.12/reference/annotations-reference.html#discriminatormap

@mpdude
Copy link
Contributor

mpdude commented Apr 25, 2023

Having all classes in the DM was a recommendation given (eg at #8736 (comment)) to work around some quirks when not all subclasses were in the $subClasses list. Documentation and some exception messages in the codebase were contradictory on this.

#10411 was a change to fill in the missing subclasses automatically, so that only non-abstract entity classes need to be listed in the DM.

@htto
Copy link
Author

htto commented Apr 27, 2023

Ah, thanks for the explanation and clarification. We probably just had luck for all these years then 🙈

@htto
Copy link
Author

htto commented May 24, 2023

@mpdude Hey @mpdude, since the PR sits around for four weeks by now, do you by chance have any idea if I need to mention/add someone to the PR and if so, who?

@mpdude
Copy link
Contributor

mpdude commented May 24, 2023

@greg0ire can you help us to get this along?

Changes are reasonable from my POV.

Do you see anything that should be addressed?

@greg0ire greg0ire added the Bug label May 24, 2023
@greg0ire greg0ire merged commit d831c12 into doctrine:2.14.x May 24, 2023
41 checks passed
@greg0ire
Copy link
Member

Thanks @htto! And thanks @mpdude for the review!

@greg0ire greg0ire added this to the 2.15.2 milestone May 24, 2023
@greg0ire
Copy link
Member

Argh! The target branch was wrong. Let me create a merge up PR

@htto htto deleted the bugfix/sti-with-abstract-intermediate branch May 25, 2023 07:47
@htto
Copy link
Author

htto commented May 25, 2023

@mpdude @greg0ire Thanks for caring, much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2.14.2: Entity Inheritance (at least STI) with intermediate abstract class is broken
3 participants