php - 教义自定义类型,更新模式的问题
问题描述
当我尝试使用学说“bin/console dictionary:schema:update -f”更新我的架构时遇到问题
问题是
[Doctrine\DBAL\Exception]
请求的未知列类型“Finances\Bank\Infrastructure\Persistence\Doctrine\BankIdType”。您使用的任何 Doctrine 类型都必须在 \Doctrine\DBAL\Types\Type::addType() 中注册。您可以
使用 \Doctrine\DBAL\Types\Type::getTypesMap() 获得所有已知类型的列表。如果在数据库自省期间发生此错误,那么您可能忘记为 Doctrine 类型注册所有数据库类型。
使用 AbstractPlatform#registerDoctrineTypeMapping() 或让您的自定义类型实现 Type#getMappedDatabaseTypes() 。如果类型名称为空,则可能是缓存有问题或忘记了一些映射
信息。
这很奇怪,因为当我在我的模式数据库中看到时,我在评论中找到了类型,如果我删除它,我可以正常更新模式。我没有找到任何额外的配置来解决这个问题。实际上我的 config.yml 是:
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
types:
bank_id: Finances\Bank\Infrastructure\Persistence\Doctrine\BankIdType
自定义类型:
final class BankIdType extends UuidType
{
protected function typeClassName(): string
{
return BankId::class;
}
}
UuidType(抽象)
abstract class UuidType extends StringType implements DoctrineCustomType
{
abstract protected function typeClassName(): string;
public function getName(): string
{
return self::customTypeName();
}
public static function customTypeName(): string
{
return static::class;
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if(!is_null($value)){
$className = $this->typeClassName();
return new $className($value);
}else{
return $value;
}
}
/** @var Uuid $value */
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if(!is_null($value)){
return $value;
}else{
return $value;
}
}
}
实现的接口:
interface DoctrineCustomType
{
public static function customTypeName(): string;
}
这是映射“Bank.orm.xml”
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-
project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Finances\Bank\Domain\Bank">
<id name="id" type="bank_id" column="id"/>
这真让我抓狂!
解决方案
最后的解决方案是删除 mysql 数据库中的所有学说注释,并在映射配置中设置自定义类型如下:
types:
bank_id:
class: Finances\Bank\Infrastructure\Persistence\Doctrine\BankIdType
commented: false
推荐阅读
- r - 将形状文件与我自己的坐标结合起来
- javascript - 当站点导航器固定到网页顶部时调整锚点
- python - LXML etree.iterparse() 中的编码错误,但 etree.parse() 中没有
- python - 从 Google App Engine 上的网络应用程序调用 Google Cloud 功能?
- css - 为什么这个 flexbox 代码没有在我的容器中的每个子 div 元素中显示我的一半复选框?
- hibernate - 休眠会话,调用方法“isJoinedToTransaction”在没有活动事务的情况下无效(当前状态:NOT_ACTIVE)
- sql - SQL 过程错误 #1064 - SINTAX 有问题
- javascript - 更多/更少 Html 脚本失败
- php - PHP 错误:试图访问 null 类型值的数组偏移量
- azure - 如何使用托管服务标识从 azure 函数执行管道(adf v2)