Symfony2 Doctrine自定义类型会生成不需要的迁移

Gle*_*eni 6 types symfony doctrine-orm

我已经创建了一个自定义学说类型,如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;
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是当我运行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');
Run Code Online (Sandbox Code Playgroud)

Nic*_*ico 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;
}
Run Code Online (Sandbox Code Playgroud)

此外,我认为可能需要为您的自定义类型指定一个独特的名称,如:

/**
 * {@inheritdoc}
 */
public function getName()
{
    return 'datetime_utc';
}
Run Code Online (Sandbox Code Playgroud)

这些功能在doctrine> = 2.3中实现