我在http://doctrine-orm.readthedocs.org/en/latest/cookbook/working-with-datetime.html
中创建了一个自定义学说类型以下是代码:
<?php
namespace XXX\Bundle\XXXBundle\Doctrine\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateTimeType;
class UTCDateTimeType extends DateTimeType
{
static private $utc = null;
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}
$value->setTimezone(new \DateTimeZone('UTC'));
$dbDate = $value->format($platform->getDateTimeFormatString());
return $dbDate;
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}
$val = \DateTime::createFromFormat(
$platform->getDateTimeFormatString(),
$value,
(self::$utc) ? self::$utc : (self::$utc = new \DateTimeZone('UTC'))
);
if (!$val) {
throw ConversionException::conversionFailed($value, $this->getName());
}
return $val;
}
}
问题是,当我运行app/console doctrine:migrations:diff
时,即使我已迁移,它也始终会生成新的迁移,并且内容始终相同。例如:
$this->addSql('ALTER TABLE Availability CHANGE start start DATETIME NOT NULL, CHANGE end end DATETIME NOT NULL, CHANGE rrule rrule LONGTEXT DEFAULT NULL, CHANGE created created DATETIME NOT NULL, CHANGE updated updated DATETIME NOT NULL');
答案 0 :(得分:9)
以下是来自此错误报告的SteveMüller的回复:http://www.doctrine-project.org/jira/browse/DBAL-1085
我认为您必须将自定义类型标记为需要SQL 注释,否则架构管理器无法区分 DateTime类型和您的自定义类型,因为它们都映射到相同的本机 SQL类型。
见这里: https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Types/Type.php#L327-L340
您必须将以下内容添加到自定义类型实现中:
/** * {@inheritdoc} */ public function requiresSQLCommentHint(AbstractPlatform $platform) { return true; }
此外,我认为可能需要为您的自定义类型指定一个不同的名称,如:
/** * {@inheritdoc} */ public function getName() { return 'datetime_utc'; }
在doctrine&gt; = 2.3
中实现了这些功能