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

Ignore subclasses without discriminatorValue when generating discriminator column condition SQL #11200

Merged
merged 9 commits into from
Feb 3, 2024

Conversation

DemoniacDeath
Copy link
Contributor

@DemoniacDeath DemoniacDeath commented Jan 31, 2024

After commit 4e8e3ef when \Doctrine\ORM\Query\SqlWalker generates dicsriminator column condition SQL (method \Doctrine\ORM\Query\SqlWalker::generateDiscriminatorColumnConditionSQL) it adds an empty string to the list of possible values if the inheritance hierarchy contains a non-root abstract class.

When the discriminator column is implemented with a custom type in PostgreSQL (equivalent of Enum) the query fails because the type cannot have a value of an empty string. It boils down to the fact that \Doctrine\ORM\Mapping\ClassMetadataInfo::$subClasses contains an abstract class and in its Metadata the value of \Doctrine\ORM\Mapping\ClassMetadataInfo::$discriminatorValue is null.

Previous behavior

In version 2.14.1 \Doctrine\ORM\Mapping\ClassMetadataInfo::$subClasses does not contain an abstract class.

Fixes #11199, fixes #11177, fixes #10846.

@DemoniacDeath DemoniacDeath changed the title Failing test for discriminator column condition SQL (#11199) Ignore subclasses without discriminatorValue when generating discriminator column condition SQL (fix for #11199) Jan 31, 2024
@mpdude
Copy link
Contributor

mpdude commented Jan 31, 2024

I have a suggestion how I'd like to change your PR here:

DemoniacDeath#1

If you like, you should be able to merge that PR into your own branch and it should be updated here.

Basically, I'd prefer to not check for null discriminator values, but instead check the fact that a class is abstract – like it is done when building or validating the discriminator map.

Additionally, I discovered the edge case that you might end up with no possible class at all.

@mpdude mpdude changed the title Ignore subclasses without discriminatorValue when generating discriminator column condition SQL (fix for #11199) Ignore subclasses without discriminatorValue when generating discriminator column condition SQL Feb 1, 2024
mpdude
mpdude previously approved these changes Feb 1, 2024
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.

LGTM!

@mpdude
Copy link
Contributor

mpdude commented Feb 1, 2024

Maintainers: We probably should squash-merge this, keeping the text from the initial comment as the commit message.

@mpdude
Copy link
Contributor

mpdude commented Feb 2, 2024

Does this also fix #11177?

@mpdude
Copy link
Contributor

mpdude commented Feb 2, 2024

#10846 is probably also the same

@plantas
Copy link

plantas commented Feb 2, 2024

This PR fixes #11177 too 👍

@mpdude mpdude self-requested a review February 3, 2024 10:08
@mpdude mpdude dismissed their stale review February 3, 2024 10:09

Rethinking this, not sure if the isAbstract check is really the best way to go.

@mpdude
Copy link
Contributor

mpdude commented Feb 3, 2024

  • What if the class metadata is taken from the cache - may that cause reflection not to be initialized, and may that lead to wrong results?
  • When metadata comes from the cache, does the reflection-based check trigger the autoloader/class load that we might want to avoid?

-> Looking at the discriminator value might be a better way to go.

On the other hand, there may be users that put abstract classes into the Discriminator Map, which was a recommendation for some time... would it make sense to skip those classes? Probably we can neglect the performance difference.

Are there any other reasons why the discriminator value might be null, can the Discriminator Map be mis-configured in a stupid way?

@mpdude
Copy link
Contributor

mpdude commented Feb 3, 2024

I was unable to get null values into the DM through configuration, it always ended up as the 0 key or a 'null' string. So, null seems to be a safe indication that the class is not in the map.

The map is checked at load time that all intermediate, non-abstract entity classes have been declared, so this seems to be safe.

mpdude
mpdude previously approved these changes Feb 3, 2024
@mpdude
Copy link
Contributor

mpdude commented Feb 3, 2024

#10643 is a very similar fix in another place… does this show additional refactorings would be appropriate?

#11213

@mpdude mpdude changed the base branch from 2.17.x to 2.18.x February 3, 2024 22:17
@mpdude mpdude merged commit 6f98147 into doctrine:2.18.x Feb 3, 2024
58 checks passed
@mpdude
Copy link
Contributor

mpdude commented Feb 3, 2024

@DemoniacDeath congratulations, you’re now a Doctrine ORM contributor. Keep it up!

🚀

@Legion112
Copy link

Big shout out to the contributors 🙇‍♂️

derrabus added a commit to derrabus/orm that referenced this pull request Feb 7, 2024
* 2.18.x:
  Point link to correct upgrade guide (doctrine#11220)
  Ignore subclasses without discriminatorValue when generating discriminator column condition SQL (doctrine#11200)
  Update branches in README
derrabus added a commit to derrabus/orm that referenced this pull request Feb 7, 2024
* 3.0.x:
  Revert "Merge pull request doctrine#11229 from greg0ire/add-columns"
  Add columns for 3.1.x and 4.0x
  Update version ORM from 2 to 3 in docs (doctrine#11221)
  Clean up outdated sentence (doctrine#11224)
  Update README.md
  Point link to correct upgrade guide (doctrine#11220)
  Ignore subclasses without discriminatorValue when generating discriminator column condition SQL (doctrine#11200)
  Update branches in README
derrabus added a commit to derrabus/orm that referenced this pull request Feb 7, 2024
* 3.1.x:
  Add TokenType class (doctrine#11228)
  Revert "Merge pull request doctrine#11229 from greg0ire/add-columns"
  Add columns for 3.1.x and 4.0x
  Update version ORM from 2 to 3 in docs (doctrine#11221)
  Clean up outdated sentence (doctrine#11224)
  Update README.md
  Point link to correct upgrade guide (doctrine#11220)
  Ignore subclasses without discriminatorValue when generating discriminator column condition SQL (doctrine#11200)
  Update branches in README
@DemoniacDeath DemoniacDeath deleted the issue-11199 branch February 16, 2024 08:44
@derrabus derrabus added the Bug label Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
6 participants