Symfony doctrine migration fails when using custom mapping type

293 views Asked by At

I try to introduce a custom mapping type but it fails in the following way:

1064 You have an error in your SQL syntax; check the manual that corresponds 
to your MySQL server version for the right syntax to use near 
'localizedstring NOT NULL COMMENT '(DC2Type:localizedstring)', 
PRIMARY KEY(id)) D' at line 1

This is my type class:

namespace App\Orm\Custom\MappingTypes;

[...]

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;

[...]

class LocalizedStringType extends Type
{
    const LOCALIZED_STRING = 'localizedstring';

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

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
       [...]
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
       [...]
    }

    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
    {
        return 'localizedstring';
    }

    public function getName(): string
    {
        return self::LOCALIZED_STRING;
    }
}

This my doctrine.yaml:

doctrine:
    dbal:
        url: '%env(resolve:DATABASE_URL)%'
        types:
            localizedstring: App\Orm\Custom\MappingTypes\LocalizedStringType

    orm:
        auto_generate_proxy_classes: true
        naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
        auto_mapping: true
        mappings:
            App:
                is_bundle: false
                type: annotation
                dir: '%kernel.project_dir%/src/Entity'
                prefix: 'App\Entity'
                alias: App

I am using:

  • Symfony 5.1
  • Doctrine 2.7.3

Any hints are appreciated. If more information is needed, I will gladly provide it.

1

There are 1 answers

0
Wolfone On BEST ANSWER

Error was due to me misunderstanding how this actually works and reading not sufficiently generic tutorials:

Solution -> change method getSQLDeclaration(...):

public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
    return 'VARCHAR(256) COMMENT "localizedstring"';
}

With this requiresSQLCommentHint(...) can also be deleted.