首页 > 解决方案 > 使用自定义映射类型时 Symfony 学说迁移失败

问题描述

我尝试引入自定义映射类型,但它以下列方式失败:

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

这是我的类型类:

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;
    }
}

这是我的学说.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

我在用:

任何提示表示赞赏。如果需要更多信息,我很乐意提供。

标签: doctrinesymfony5

解决方案


错误是由于我误解了这实际上是如何工作的并且阅读了不够通用的教程:

解决方案 -> 更改方法 getSQLDeclaration(...):

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

有了这个requiresSQLCommentHint(...)也可以被删除。


推荐阅读