gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:12:24 25 4
gpt4 key购买 nike

我创建了一个自定义的教义类型,如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');

最佳答案

这是SteveMüller对此错误报告的回应:http://www.doctrine-project.org/jira/browse/DBAL-1085

I think you will have to mark your custom type as requiring a SQL comment, otherwise the schema manager cannot distinguish between DateTime type and your custom type because both map to the same native SQL type.

See here: https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Types/Type.php#L327-L340

You will have to add the following to your custom type implementation:

/**
* {@inheritdoc}
*/
public function requiresSQLCommentHint(AbstractPlatform $platform)
{
return true;
}

Also I think it might be required to give your custom type a distinct name like:

/**
* {@inheritdoc}
*/
public function getName()
{
return 'datetime_utc';
}


在> = 2.3的学说中实现了某些功能

关于Symfony2 Doctrine自定义类型生成不需要的迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26964367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com