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

Allow using Enum from different namespace than Entity #9384

Merged
merged 1 commit into from
Jan 16, 2022

Conversation

javer
Copy link
Contributor

@javer javer commented Jan 15, 2022

Allows using Enums from different namespace than Entity, for example, App\DBAL\Enum and App\Entity.
Without this fix it works only when both Enum and Entity are placed in the same namespace like App\Entity, and nobody is responsible for autoloading these Enums.

@@ -1517,7 +1517,7 @@ private function validateAndCompleteTypedFieldMapping(array $mapping): array
! isset($mapping['type'])
&& ($type instanceof ReflectionNamedType)
) {
if (PHP_VERSION_ID >= 80100 && ! $type->isBuiltin() && enum_exists($type->getName(), false)) {
if (PHP_VERSION_ID >= 80100 && ! $type->isBuiltin() && enum_exists($type->getName())) {
Copy link
Member

Choose a reason for hiding this comment

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

Not sure why autoload was disabled in the first place. @beberlei , do you remember?

Copy link
Member

@derrabus derrabus Jan 15, 2022

Choose a reason for hiding this comment

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

Triggering the autoloader here is correct. We cannot be certain at this point that the property type has been autoloaded already.

However, this has nothig little to do with the namespace we place the enum in.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does, I've checked this before writing it: with disabled autoloading Enum from the Entity namespace is somehow visible, that's why unit tests working with Enums\TypedCard are green. But moving Enum to another namespace break things.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can confirm we need removing false. Works for me now.

@@ -1517,7 +1517,7 @@ private function validateAndCompleteTypedFieldMapping(array $mapping): array
! isset($mapping['type'])
&& ($type instanceof ReflectionNamedType)
) {
if (PHP_VERSION_ID >= 80100 && ! $type->isBuiltin() && enum_exists($type->getName(), false)) {
if (PHP_VERSION_ID >= 80100 && ! $type->isBuiltin() && enum_exists($type->getName())) {
Copy link
Member

@derrabus derrabus Jan 15, 2022

Choose a reason for hiding this comment

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

Triggering the autoloader here is correct. We cannot be certain at this point that the property type has been autoloaded already.

However, this has nothig little to do with the namespace we place the enum in.

@derrabus derrabus added this to the 2.11.1 milestone Jan 15, 2022
@derrabus derrabus added the Bug label Jan 15, 2022
@javer
Copy link
Contributor Author

javer commented Jan 16, 2022

So do I need to write some test for this or is the fix enough obvious? It's hard to write autoloading tests, because there is no guarantee that someone else doesn't loaded your class already.

@beberlei
Copy link
Member

Fix is obvious

@beberlei beberlei merged commit 01c1644 into doctrine:2.11.x Jan 16, 2022
@javer javer deleted the enum-exists-enable-autoload branch January 16, 2022 14:27
derrabus added a commit to derrabus/orm that referenced this pull request Jan 17, 2022
* 2.12.x:
  Allow using Enum from different namespace than Entity (doctrine#9384)
  Corrected ORM version and added missing dependency (doctrine#9386)
  PHPStan 1.4.0 (doctrine#9385)
  [doctrineGH-9380] Bugfix: Delegate ReflectionEnumProperty::getAttributes(). (doctrine#9381)
  Support enum cases as parameters (doctrine#9373)
  Add detach as of list cascade-all operations (doctrine#9357)
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.

5 participants