我在这件事上浪费了 3 个小时,仍然无法弄清楚出了什么问题,我正在尝试添加一个 VARBINARY 字段类型,并且学说在生成新实体时向我显示了该字段作为一个选项,但问题是当我这样做时php app/console doctrine:schema:update --force
它给了我一个错误,
[DoctrineDBAL\DBALException]
An exception occured while executing 'CREATE TABLE Test (id INT AUTO_INCREMENT NOT NULL, name VARBINARY NOT NULL, PRIMARY KEY(id))
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;check the manual that corresponds to your MySQL server version for the right sytax to use near 'NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci EN' at line 1
这是我的 VARBINARY 类型类
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class VarBinaryType extends Type {
const VARBINARY = 'VARBINARY';
public function getName()
{
return self::VARBINARY;
}
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) {
return $platform->getDoctrineTypeMapping('VARBINARY');
}
public function convertToDatabaseValue($value, AbstractPlatform $platform) {
return ($value === null)? null : base64_encode($value);
}
public function convertToPHPValue($value, AbstractPlatform $platform) {
return ($value === null)? null : base64_decode($value);
}
}
我也确实在引导功能中注册了它
$connection = $this->container->get('doctrine.orm.entity_manager')->getConnection();
if(!Type::hasType('VARBINARY'))
{
Type::addType('VARBINARY', 'Uapi\\CoreBundle\\System\\DBALType\\VarBinaryType');
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('VARBINARY', 'VARBINARY');
}