doctrine - 使用自定义映射类型时 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
我在用:
- Symfony 5.1
- 教义 2.7.3
任何提示表示赞赏。如果需要更多信息,我很乐意提供。
解决方案
错误是由于我误解了这实际上是如何工作的并且阅读了不够通用的教程:
解决方案 -> 更改方法 getSQLDeclaration(...):
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return 'VARCHAR(256) COMMENT "localizedstring"';
}
有了这个requiresSQLCommentHint(...)也可以被删除。
推荐阅读
- c# - 我无法通过调整大小来减小表单的大小
- java - Convert String to packed decimal in java
- c# - Unable to use server command from client using unity mirror
- google-sheets-formula - 使用 2 个条件过滤
- python - Exclude x window using matplotlib fill_between
- swift - How to mock inheritance in a swift struct
- user-interface - How to add border to RoundedRectangle in flutter
- reactjs - Change a property of an object inside the Redux state object
- swift - Swift UI 中没有 Scene 和 App 委托
- amazon-web-services - How to run tensorboard for tensorflow in AWS Sagemaker?