我正在尝试将自定义类型映射到字符串。下面是我的实体定义:
/**
* @var string
*
* @ORM'Column(name="type", type="string", columnDefinition="my_type_enum", nullable=false)
*/
但是当我尝试创建迁移(migration:diff)时,这是输出
[学说' DBAL ' DBALException]未知数据库类型my_type_enum
请求,Doctrine'DBAL'Platforms'PostgreSQL92Platform
可能不支持rt。
似乎我需要将我的自定义类型my_type_enum
映射到使用mapping_types
的字符串,但在Zend表达的地方?似乎我的配置被忽略了
...
'doctrine' => [
'dbal' => [
'mapping_types' => [
'my_type_enum' => 'string'
]
]
]
...
zend-expressive本身并没有内置原则支持。这取决于你正在使用的学说模块和它的工厂。工厂用下面的配置启动doctrine服务。因此,我将查看原则工厂内部,以弄清楚它如何以及是否支持自定义映射类型。
如果您的容器不支持它,您可以使用容器互操作原则。它似乎内置了这个支持(我自己没有尝试过):
<?php
return [
'doctrine' => [
// ...
'connection' => [
'orm_default' => [
'driver_class' => 'Doctrine'DBAL'Driver'PDOMySql'Driver::class,
'wrapper_class' => null,
'pdo' => null,
'configuration' => 'orm_default',
'event_manager' => 'orm_default',
'params' => [],
'doctrine_mapping_types' => [], // <-----
'doctrine_commented_types' => [],
],
],
'types' = [
'typename' => Type::class,
], // <-----
],
];
首先,您必须创建扩展doctrine DBAL类型的自定义类型:
<?php
use Doctrine'DBAL'Types'Type;
use Doctrine'DBAL'Platforms'AbstractPlatform;
use Doctrine'DBAL'Types'ConversionException;
class MyType extends Type
{
const MYTYPE = 'mytype';
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) {
return 'mytype';
}
public function convertToPHPValue($value, AbstractPlatform $platform) {
// convert your type to php value
}
public function convertToDatabaseValue($value, AbstractPlatform $platform) {
// convert your type to database value
}
}
最近我集成了一个值对象作为原则类型,所以你可以看看你的新类型应该是什么样子的:
下一步是注册新类型,比如在你的理论引导或EntityManagerFactory中:
<?php // ./src/Container/EntityManagerFactory.php
if (!'Doctrine'DBAL'Types'Type::hasType("mytype")) {
'Doctrine'DBAL'Types'Type::addType('mytype', 'Your'Namespace'MyType');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('mytype', 'mytype');
}
return $em;
最后你已经注册了你的新类型,你可以使用它了:
/**
* @var 'Your'Namespace'MyType
* @Column(type="mytype")
*/
protected $param;