Hydration error for int-backed enums stored as string values in MySQL ENUM columns #7577

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

Originally created by @TheBreaken on GitHub (Nov 17, 2025).

Bug Report

Summary

When using PHP backed enums with Doctrine, a type mismatch between the database column and the PHP enum unexpectedly triggers an error.
This occurs when the database stores enum values as strings (e.g., MySQL ENUM or VARCHAR) but the PHP enum uses integers as its backed type.

Example use case: MySQL ENUM columns containing internal codes like 500 or 510 are stored as strings, while the corresponding PHP enum uses integer backed values.

Doctrine currently does not cast these values and fails when trying to hydrate the enum.

Current behavior

If the database column contains a value like "500" (string), but the PHP enum expects an integer (e.g., 500), Doctrine throws an error during hydration:

  • Doctrine does not cast the value
  • Doctrine tries to hydrate the enum using the string "500"
  • This results in a ValueError or similar enum resolution failure

Expected behavior

Doctrine should:

  • Detect that the enum’s backed type is int
  • Automatically cast the incoming database value to int when possible
  • Hydrate the enum correctly without error
Originally created by @TheBreaken on GitHub (Nov 17, 2025). ### Bug Report #### Summary When using PHP backed enums with Doctrine, a type mismatch between the database column and the PHP enum unexpectedly triggers an error. This occurs when the database stores enum values as strings (e.g., MySQL ENUM or VARCHAR) but the PHP enum uses integers as its backed type. Example use case: MySQL ENUM columns containing internal codes like 500 or 510 are stored as strings, while the corresponding PHP enum uses integer backed values. Doctrine currently does not cast these values and fails when trying to hydrate the enum. #### Current behavior If the database column contains a value like "500" (string), but the PHP enum expects an integer (e.g., 500), Doctrine throws an error during hydration: - Doctrine does not cast the value - Doctrine tries to hydrate the enum using the string "500" - This results in a ValueError or similar enum resolution failure #### Expected behavior Doctrine should: - Detect that the enum’s backed type is int - Automatically cast the incoming database value to int when possible - Hydrate the enum correctly without error
admin closed this issue 2026-01-22 15:53:43 +01:00
Author
Owner

@derrabus commented on GitHub (Nov 18, 2025):

No tests?

@derrabus commented on GitHub (Nov 18, 2025): No tests?
Author
Owner

@TheBreaken commented on GitHub (Nov 18, 2025):

@derrabus I added now some tests in the PR https://github.com/doctrine/orm/pull/12275

@TheBreaken commented on GitHub (Nov 18, 2025): @derrabus I added now some tests in the PR https://github.com/doctrine/orm/pull/12275
Author
Owner

@derrabus commented on GitHub (Nov 18, 2025):

Right. Sorry, I don't know how my review comment ended up on the bug report instead of the PR. 🙈

@derrabus commented on GitHub (Nov 18, 2025): Right. Sorry, I don't know how my review comment ended up on the bug report instead of the PR. 🙈
Author
Owner

@TheBreaken commented on GitHub (Nov 27, 2025):

Hello @derrabus can you review the PR?

@TheBreaken commented on GitHub (Nov 27, 2025): Hello @derrabus can you review the PR?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7577