Doctrine DBAL, diff command and enum type

I am working with symfony 5.1, doctrine-bundle 2.1.2 and doctrine-migrations-bundle 2.2. I am NOT working with ORM and define my own Schemas. To add Enum types, I am using the following code:

abstract class EnumType extends Type
{
    protected string $name;

    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        $values = $this->getValues();
        $maxLength = max(array_map('strlen', $values));
        $columnName = $fieldDeclaration['name'];

        $implodedValues = implode(', ', array_map(function($value) {return "'$value'";}, $values));

        if ($platform instanceof MySqlPlatform) {
            return "ENUM($implodedValues)";
        }

        if (
            $platform instanceof SQLServer2012Platform
            || $platform instanceof PostgreSQL94Platform
        ) {
            return "VARCHAR($maxLength) CHECK ({$columnName} IN ($implodedValues))";
        }

        if ($platform instanceof SqlitePlatform) {
            return "TEXT CHECK ({$columnName} IN ($implodedValues))";
        }

        throw DBALException::invalidPlatformType($platform);
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        return $value;
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        if (!in_array($value, $this->getValues())) {
            throw new InvalidArgumentException("Invalid '" . $this->name . "' value: " . (string)$value);
        }
        return $value;
    }

    public function getName()
    {
        return $this->name;
    }

    public function requiresSQLCommentHint(AbstractPlatform $platform)
    {
        return true;
    }

    abstract function getValues(): array;
}

Each enum then extends this abstract class to set the values.

Creating is no issue. When I run the migration diff command, I am getting the following error message:

Unknown database type enum requested, DoctrineDBALPlatformsMySQL57Platform may not support it.

Any ideas how I can create a diff that also includes any changes to the enum itself?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *