我在這件事上失去了3個小時,仍然無法弄清楚什麼是錯的,iam試圖添加一個VARBINARY字段類型,並且doctrine在生成新實體時顯示該字段爲選項,但是問題是,當我這樣做php app/console doctrine:schema:update --force
那麼它給了我一個錯誤,Symfony2 add new doctrine字段類型
[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');
}
感謝您的回覆,現在我想goig再花3個小時試圖找出如何添加字段長度順便說一句。文檔很糟糕 – user3035443