You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/reference/basic-mapping.rst
+155-1Lines changed: 155 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -240,7 +240,7 @@ Here is a complete list of ``Column``s attributes (all optional):
240
240
- ``insertable`` (default: ``true``): Whether the column should be inserted.
241
241
- ``updatable`` (default: ``true``): Whether the column should be updated.
242
242
- ``generated`` (default: ``null``): Whether the generated strategy should be ``'NEVER'``, ``'INSERT'`` and ``ALWAYS``.
243
-
- ``enumType`` (requires PHP 8.1 and ``doctrine/orm`` 2.11): The PHP enum class name to convert the database value into.
243
+
- ``enumType`` (requires PHP 8.1 and ``doctrine/orm`` 2.11): The PHP enum class name to convert the database value into. See :ref:`reference-enum-mapping`.
244
244
- ``precision`` (default: 0): The precision for a decimal (exact numeric) column
245
245
(applies only for decimal column),
246
246
which is the maximum number of digits that are stored for the values.
@@ -311,6 +311,160 @@ and a custom ``Doctrine\ORM\Mapping\TypedFieldMapper`` implementation.
311
311
312
312
:doc:`Read more about TypedFieldMapper <typedfieldmapper>`.
313
313
314
+
.. _reference-enum-mapping:
315
+
316
+
Mapping PHP Enums
317
+
-----------------
318
+
319
+
.. versionadded:: 2.11
320
+
321
+
Doctrine natively supports mapping PHP backed enums to database columns.
322
+
A backed enum is a PHP enum that the same scalar type (``string`` or ``int``)
323
+
assigned to each case. Doctrine stores the scalar value in the database and
324
+
converts it back to the enum instance when hydrating the entity.
325
+
326
+
Using ``enumType`` provides three main benefits:
327
+
328
+
- **Automatic conversion**: Doctrine handles the conversion in both directions
329
+
transparently. When loading an entity, scalar values from the database are
330
+
converted into enum instances. When persisting, enum instances are reduced
331
+
to their scalar ``->value`` before being sent to the database.
332
+
- **Type-safety**: Entity properties contain enum instances directly. Your
333
+
getters return ``Suit`` instead of ``string``, removing the need to call
334
+
``Suit::from()`` manually.
335
+
- **Validation**: When a database value does not match any enum case, Doctrine
336
+
throws a ``MappingException`` during hydration instead of silently returning
337
+
an invalid value.
338
+
339
+
This feature works with all database platforms supported by Doctrine (MySQL,
340
+
PostgreSQL, SQLite, etc.) as it relies on standard column types (``string``,
341
+
``integer``, ``json``, ``simple_array``) rather than any vendor-specific enum
342
+
type.
343
+
344
+
.. note::
345
+
346
+
This is unrelated to the MySQL-specific ``ENUM`` column type covered in
347
+
:doc:`the MySQL Enums cookbook entry </cookbook/mysql-enums>`.
348
+
349
+
Defining an Enum
350
+
~~~~~~~~~~~~~~~~
351
+
352
+
.. literalinclude:: basic-mapping/Suit.php
353
+
:language: php
354
+
355
+
Only backed enums (``string`` or ``int``) are supported. Unit enums (without
356
+
a scalar value) cannot be mapped.
357
+
358
+
Single-Value Columns
359
+
~~~~~~~~~~~~~~~~~~~~
360
+
361
+
Use the ``enumType`` option on ``#[Column]`` to map a property to a backed enum.
362
+
The underlying database column stores the enum's scalar value (``string`` or ``int``).
363
+
364
+
.. literalinclude:: basic-mapping/EnumMapping.php
365
+
:language: php
366
+
367
+
When the PHP property is typed with the enum class, Doctrine automatically
368
+
infers the appropriate column type (``string`` for string-backed enums,
369
+
``integer`` for int-backed enums) and sets ``enumType``. You can also specify
370
+
the column ``type`` explicitly.
371
+
372
+
Storing Collections of Enums
373
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
374
+
375
+
You can store multiple enum values in a single column by combining ``enumType``
376
+
with a collection column type: ``json`` or ``simple_array``.
377
+
378
+
.. note::
379
+
380
+
Automatic type inference does not apply to collection columns. When the
381
+
PHP property is typed as ``array``, Doctrine cannot detect the enum class.
382
+
You must specify both ``type`` and ``enumType`` explicitly.
0 commit comments