使用自定义映射类型时 Symfony 原则迁移失败
Symfony doctrine migration fails when using custom mapping type
我尝试引入自定义映射类型,但失败如下:
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
这是我喜欢的类型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;
}
}
这是我的 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
我正在使用:
- Symfony 5.1
- 学说 2.7.3
如有任何提示,我们将不胜感激。如果需要更多信息,我很乐意提供。
错误是由于我误解了它的实际工作原理并且阅读了不够通用的教程:
解决方案 -> 更改方法 getSQLDeclaration(...):
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return 'VARCHAR(256) COMMENT "localizedstring"';
}
有了这个requiresSQLCommentHint(...)也可以删除
我尝试引入自定义映射类型,但失败如下:
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
这是我喜欢的类型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;
}
}
这是我的 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
我正在使用:
- Symfony 5.1
- 学说 2.7.3
如有任何提示,我们将不胜感激。如果需要更多信息,我很乐意提供。
错误是由于我误解了它的实际工作原理并且阅读了不够通用的教程:
解决方案 -> 更改方法 getSQLDeclaration(...):
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return 'VARCHAR(256) COMMENT "localizedstring"';
}
有了这个requiresSQLCommentHint(...)也可以删除