[英]zend expressive + doctrine custom types
我正在嘗試將自定義類型映射到字符串。 這是我的實體定義:
/**
* @var string
*
* @ORM\Column(name="type", type="string", columnDefinition="my_type_enum", nullable=false)
*/
但是,當我嘗試創建遷移(migration:diff)時,這是輸出
[Doctrine \\ DBAL \\ DBALException]請求了未知的數據庫類型my_type_enum
, Doctrine\\DBAL\\Platforms\\PostgreSQL92Platform
可能不支持它。
似乎我需要使用mapping_types
將自定義類型my_type_enum
到字符串,但是Zend Expressive中的何處? 看來我的配置被忽略了
...
'doctrine' => [
'dbal' => [
'mapping_types' => [
'my_type_enum' => 'string'
]
]
]
...
zend-expressive本身沒有內置的學說支持。它取決於您所使用的學說模塊及其工廠。 工廠使用配置啟動理論服務。 因此,我將深入研究原則工廠,以了解其如何以及是否支持自定義映射類型。
如果您的服務器不支持它,則可以使用container-interop-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,
], // <-----
],
];
首先,您必須創建擴展了理論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
}
}
最近,我將值對象集成為一種學說類型,因此您可以看一下新類型的外觀: PostcodeType
下一步是注冊新類型,在您的學說引導程序或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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.