首先,您必须创建扩展学说 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;