Doctrine 自定义类型,更新模式时出现问题
Doctrine custom types, problems updating schema
当我尝试使用学说“bin/console doctrine:schema:update -f”更新我的架构时遇到问题
问题是
[Doctrine\DBAL\Exception]
Unknown column type "Finances\Bank\Infrastructure\Persistence\Doctrine\BankIdType" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a lis
t of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgotten to register all database types for a Doctrine Type. U
se AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping
information.
这很奇怪,因为当我在我的模式数据库中看到时,我在评论中找到了类型,如果我删除它,我可以正常更新模式。我没有找到任何其他配置来解决这个问题。
实际上我的 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
schema detail
自定义类型:
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
当我尝试使用学说“bin/console doctrine:schema:update -f”更新我的架构时遇到问题
问题是
[Doctrine\DBAL\Exception]
Unknown column type "Finances\Bank\Infrastructure\Persistence\Doctrine\BankIdType" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a lis
t of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgotten to register all database types for a Doctrine Type. U
se AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping
information.
这很奇怪,因为当我在我的模式数据库中看到时,我在评论中找到了类型,如果我删除它,我可以正常更新模式。我没有找到任何其他配置来解决这个问题。 实际上我的 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
schema detail
自定义类型:
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