Enum field mapping validation #7240

Closed
opened 2026-01-22 15:47:43 +01:00 by admin · 5 comments
Owner

Originally created by @yceruto on GitHub (Nov 2, 2023).

The recently properties types validation implemented in https://github.com/doctrine/orm/pull/10946 is not working properly for enum mappings:

$ bin/console doctrine:schema:validate

Mapping

[FAIL] The entity-class App\Model\Product mapping is invalid:

  • The field 'App\Model\Product#status' has the property type 'App\Model\ProductStatus' that differs from the metadata field type 'string' returned by the 'string' DBAL type.

16028e4fd3/lib/Doctrine/ORM/Tools/SchemaValidator.php (L384-L387)

where $propertyType is a BackedEnum class and $metadataFieldType is string or int.

Possible solution

I'd add another check (just below that block) to validate the enum mapping, something like this worked for me:

if (
    is_a($propertyType, BackedEnum::class, true)
    && isset($fieldMapping['enumType'])
    && $propertyType === $fieldMapping['enumType']
    && $metadataFieldType === get_debug_type($propertyType::cases()[0]->value)
) {
    return null;
}

It checks the property type against the enumType instead, but also checks if the BackedEnum variant (string or int) matches the metadata field type.

Does it make sense to you?

Affected versions: 2.17.x+

Originally created by @yceruto on GitHub (Nov 2, 2023). The recently properties types validation implemented in https://github.com/doctrine/orm/pull/10946 is not working properly for enum mappings: >$ bin/console doctrine:schema:validate > >Mapping > > [FAIL] The entity-class App\Model\Product mapping is invalid: > * The field 'App\Model\Product#status' has the property type 'App\Model\ProductStatus' that differs from the metadata field type 'string' returned by the 'string' DBAL type. https://github.com/doctrine/orm/blob/16028e4fd3202e21cecbd87071e46b40f7eda8bf/lib/Doctrine/ORM/Tools/SchemaValidator.php#L384-L387 where `$propertyType` is a BackedEnum class and `$metadataFieldType` is `string` or `int`. **Possible solution** I'd add another check (just below that block) to validate the enum mapping, something like this worked for me: ```php if ( is_a($propertyType, BackedEnum::class, true) && isset($fieldMapping['enumType']) && $propertyType === $fieldMapping['enumType'] && $metadataFieldType === get_debug_type($propertyType::cases()[0]->value) ) { return null; } ``` It checks the property type against the `enumType` instead, but also checks if the BackedEnum variant (string or int) matches the metadata field type. Does it make sense to you? Affected versions: `2.17.x`+
admin added the Bug label 2026-01-22 15:47:43 +01:00
admin closed this issue 2026-01-22 15:47:43 +01:00
Author
Owner

@derrabus commented on GitHub (Nov 2, 2023):

Can you provide a reproducer or a failing test for your issue? If you've already found a solution (as you apparently did), don't hesitate to open a PR with your fix and a test that fails without your fix.

@derrabus commented on GitHub (Nov 2, 2023): Can you provide a reproducer or a failing test for your issue? If you've already found a solution (as you apparently did), don't hesitate to open a PR with your fix and a test that fails without your fix.
Author
Owner

@yceruto commented on GitHub (Nov 3, 2023):

Here we go https://github.com/doctrine/orm/pull/11039

@yceruto commented on GitHub (Nov 3, 2023): Here we go https://github.com/doctrine/orm/pull/11039
Author
Owner

@W0rma commented on GitHub (Nov 16, 2023):

I stumbled over this issue when using doctrine/orm:2.17.

Seems like 2.17 was released before this issue has been fixed.

@W0rma commented on GitHub (Nov 16, 2023): I stumbled over this issue when using `doctrine/orm:2.17`. Seems like `2.17` was released before this issue has been fixed.
Author
Owner

@delolmo commented on GitHub (Nov 16, 2023):

Having the same problem here when updating to 2.17. When trying to validate the mappings, all enumTypes started failing. They didn't on previous versions.

@delolmo commented on GitHub (Nov 16, 2023): Having the same problem here when updating to `2.17`. When trying to validate the mappings, all enumTypes started failing. They didn't on previous versions.
Author
Owner

@stlrnz commented on GitHub (Nov 16, 2023):

I can confirm this issue.

@stlrnz commented on GitHub (Nov 16, 2023): I can confirm this issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7240