【问题标题】:Doctrine 2: Can't update DateTime column on SQL Server 2008apm原则 2:无法更新 SQL Server 2008apm 上的 DateTime 列
【发布时间】:2012-05-18 16:44:32
【问题描述】:

我在 apache 服务器上使用 Doctrine 2.2 和 php 5.3。

到目前为止,我偶然发现了以下问题: 当我尝试更新日期时间列时,我得到: SQLSTATE[22007]:[Microsoft][SQL Server Native Client 10.0][SQL Server]从字符串转换日期和/或时间时转换失败。

到目前为止,我什至已经进入该列,然后使用它只添加了 1 天来设置新日期......同样的结果。

当我将数据库中的列和实体中的列从日期时间更改为日期时,它按预期运行。

我的主要问题是,有几个字段我需要使用日期时间列。

这是我的代码:

(birthdate 是我更改为 date 的栏目......并且是为数不多的对我来说可能的栏目之一):

//This returns the datetime object that represents birthdate from the database 
$help=$object->getBirthDate(); 
$help->setTimestamp(mktime($time[0],$time[1],$time[2],$date[2],$date[1],$date[0])); 
$help->format(\DateTime::ISO8601); 
$object->setBirthDate($help);

有人知道这里的解决方法吗?

【问题讨论】:

    标签: php sql-server-2008 datetime doctrine doctrine-orm


    【解决方案1】:

    我在使用 Microsoft SQL Server Express 17 和 Doctrine ORM v2.6.3/DBAL v2.9.2 时仍然遇到同样的问题。 (对于Doctrine Platform,我使用SQLServer2012Platform)。

    您需要做的就是将您的 SQL 字段从 DATETIME 更改为 DATETIME2 类型(尽可能)

    【讨论】:

      【解决方案2】:

      我在 Doctrine 2.5 和 SQL Server 2012 中遇到了这个问题。问题是数据库字段类型为 DATETIME,但在 SQLServer2008Platform 及更高版本上,doctirne 仅支持 DATETIME2

      您不应该在您的供应商目录中编辑文件。正确答案是创建自定义类型:Doctrine Custom Mapping Types。就我而言,我扩展了当前的 DateTimeType:

      <?php
      
      namespace AppBundle\Doctrine\Type;
      
      use Doctrine\DBAL\Types\DateTimeType;
      use Doctrine\DBAL\Platforms\AbstractPlatform;
      
      class DateTime extends DateTimeType
      {
          private $dateTimeFormatString = 'Y-m-d H:i:s.000';
      
          public function convertToDatabaseValue($value, AbstractPlatform $platform)
          {
              return ($value !== null)
                  ? $value->format($this->dateTimeFormatString) : null;
          }
      
      }
      

      然后在 Symfony config.yml 中:

          types:
            datetime: AppBundle\Doctrine\Type\DateTime
      

      【讨论】:

      • 要在 symfony 之外应用这个类(如果直接使用doctrine-orm),只需在代码中的任何位置添加use Doctrine\DBAL\Types\Type; Type::overrideType(Type::DATETIME, 'YourNameSpace\DateTimeTypeSqlServer2012');,然后再使用任何日期条件。我将它添加到生成 EntityManager 类的函数中,因此它是一种永久性修复。
      • 对于现在熟悉 config.yml 中 YAML 配置的学说的任何人,应将 types:key 添加到学说中:dbal:key。
      【解决方案3】:

      它是 Doctrine 2.2 中的官方错误,将在 2.3 中解决。

      微秒被转换为具有错误数量的零的字符串。

      解决方法: 在文件中更改函数 convertToDatabaseValue /Doctrine/DBAL/Types/DateTimeType.php:

      public function convertToDatabaseValue($value, AbstractPlatform $platform)
      {
         if( $value === null)
            return null;
      
         $value = $value->format($platform->getDateTimeFormatString());
      
         if( strlen($value) == 26 &&
               $platform->getDateTimeFormatString() == 'Y-m-d H:i:s.u' &&
               ($platform instanceof \Doctrine\DBAL\Platforms\SQLServer2008Platform
                ||
               $platform instanceof \Doctrine\DBAL\Platforms\SQLServer2005Platform
               ) )
            $value = substr($value, 0, \strlen($value)-3);
         return $value;
      }
      

      【讨论】:

      • 3 年多过去了,DBAL 是 2.5 ......仍然没有修复 :( 在使用 SQL Server 时,我还必须在 Doctrine 1.x 中做这样的变通方法。你好?高-除非这些事情得到解决,否则像这样的质量库不会在企业中赢得信誉。抱歉咆哮,但我是那些试图在 M$ 商店中推动 OSS 的人之一。这样的问题对我的情况没有帮助.
      • 如何避免更改 Composer 下的文件?如何注册自己的类型?
      • 谢谢,这帮助我在我们的一个旧项目中修复了这个错误。
      • @DinoAmino 我现在不知道这是否对您有所帮助,但请参阅我的回答,了解如何使用 DBAL 2.5 解决此问题
      【解决方案4】:

      您将什么值传递给datetime 字段?你应该传递一个Datetime 实例

      $entity->setDate(new \Datetime());
      

      【讨论】:

      • 是的,我正在传递一个日期时间实例。我所做的如下(生日是我更改为日期的列......并且是我可能的少数列之一): $help=$object->getBirthDate(); //这会从数据库中返回代表生日的日期时间对象 $help->setTimestamp(mktime($time[0],$time[1],$time[2],$date[2],$date[1] ,$日期[0])); $help->格式(\DateTime::ISO8601); $object->setBirthDate($help);
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2015-10-08
      • 2019-09-18
      • 2012-12-28
      • 2017-02-07
      相关资源
      最近更新 更多