[PR #9382] Expose enumType to DBAL to make native DB Enum possible #11572

Open
opened 2026-01-22 16:11:11 +01:00 by admin · 0 comments
Owner

Original Pull Request: https://github.com/doctrine/orm/pull/9382

State: closed
Merged: Yes


The idea behing this is the following: by overriding DBAL built-in StringType we can use native enums directly in the database. But to make it possible we need enumType information on DBAL side, right now it's holded only in ORM mapping and isn't passed to DBAL in SchemaTool.

# config/packages/doctrine.yaml
doctrine:
    dbal:
        types:
            string: App\DBAL\Type\EnumType
# src/App/DBAL/Type/EnumType.php
<?php

namespace App\DBAL\Type;

use BackedEnum;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Types\StringType;

class EnumType extends StringType
{
    public function getSqlDeclaration(array $column, AbstractPlatform $platform): string
    {
        if ($platform instanceof MySQLPlatform && isset($column['enumType']) && enum_exists($column['enumType'])) {
            $values = array_map(static fn(BackedEnum $case) => "'{$case->value}'", $column['enumType']::cases());

            return 'ENUM(' . implode(', ', $values) . ')';
        }

        return parent::getSQLDeclaration($column, $platform);
    }
}

By doing this the following entity

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

enum Suit: string {
    case Hearts = 'H';
    case Diamonds = 'D';
    case Clubs = 'C';
    case Spades = 'S';
}

#[ORM\Entity]
class Card
{
    #[ORM\Id, ORM\GeneratedValue, ORM\Column]
    public int $id;

    #[ORM\Column]
    public Suit $suit;
}

results in the migration:

CREATE TABLE card (
    id INT AUTO_INCREMENT NOT NULL,
    suit ENUM('H', 'D', 'C', 'S') NOT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;
**Original Pull Request:** https://github.com/doctrine/orm/pull/9382 **State:** closed **Merged:** Yes --- The idea behing this is the following: by overriding DBAL built-in StringType we can use native enums directly in the database. But to make it possible we need enumType information on DBAL side, right now it's holded only in ORM mapping and isn't passed to DBAL in SchemaTool. ```yaml # config/packages/doctrine.yaml doctrine: dbal: types: string: App\DBAL\Type\EnumType ``` ```php # src/App/DBAL/Type/EnumType.php <?php namespace App\DBAL\Type; use BackedEnum; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\MySQLPlatform; use Doctrine\DBAL\Types\StringType; class EnumType extends StringType { public function getSqlDeclaration(array $column, AbstractPlatform $platform): string { if ($platform instanceof MySQLPlatform && isset($column['enumType']) && enum_exists($column['enumType'])) { $values = array_map(static fn(BackedEnum $case) => "'{$case->value}'", $column['enumType']::cases()); return 'ENUM(' . implode(', ', $values) . ')'; } return parent::getSQLDeclaration($column, $platform); } } ``` By doing this the following entity ```php <?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; enum Suit: string { case Hearts = 'H'; case Diamonds = 'D'; case Clubs = 'C'; case Spades = 'S'; } #[ORM\Entity] class Card { #[ORM\Id, ORM\GeneratedValue, ORM\Column] public int $id; #[ORM\Column] public Suit $suit; } ``` results in the migration: ```sql CREATE TABLE card ( id INT AUTO_INCREMENT NOT NULL, suit ENUM('H', 'D', 'C', 'S') NOT NULL, PRIMARY KEY(id) ) ENGINE = InnoDB; ```
admin added the pull-request label 2026-01-22 16:11:11 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#11572