首页 > 解决方案 > 教义自定义类型,更新模式的问题

问题描述

当我尝试使用学说“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"/>

这真让我抓狂!

标签: phpsymfonydoctrine-ormdoctrine

解决方案


最后的解决方案是删除 mysql 数据库中的所有学说注释,并在映射配置中设置自定义类型如下:

types:
  bank_id:
    class: Finances\Bank\Infrastructure\Persistence\Doctrine\BankIdType
    commented: false

推荐阅读