Doctrine 2:无法在SQL Server 2008apm上更新DateTime coloumn [英] Doctrine 2: Can't update DateTime coloumn on SQL Server 2008apm

查看:189
本文介绍了Doctrine 2:无法在SQL Server 2008apm上更新DateTime coloumn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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



我甚至已经到了这个列,然后使用它只添加了1天来设置新的日期......相同的结果。 p>

当我将数据库中的列和实体中的列从datetime更改为日期时,它的作用如所期望。



我的主要问题是,我需要几个字段来使用datetime列。



这是我的代码:



(birthdate是我现在更改的列.... ....是可供我使用的列之一):

  //这将返回代表birthdate的datetime对象atabase 
$ 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);

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



非常感谢任何帮助。

解决方案

Doctrine 2.2中的错误,要在2.3中解决。



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



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

  public function convertToDatabaseValue($ value,AbstractPlatform $ platform)
{
if($ value === null)
return null;

$ value = $ value-> format($ platform-> getDateTimeFormatString());

if(strlen($ value)== 26&&b $ b $ platform-> getDateTimeFormatString()=='Ymd 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;
}


I'm using Doctrine 2.2 with php 5.3 on an apache server.

So far I've stumbled upon the following problem: When I try to update a datetime column I get: SQLSTATE[22007]: [Microsoft][SQL Server Native Client 10.0][SQL Server]Conversion failed when converting date and/or time from character string.

I've even gone so far to make a get onto the column and then use that with only 1 day added to it to set the new date......same result.

When I instead change both the column in the database and in the entity from datetime to date, it functions as intended.

My main problem is, that there are a few fields where I will NEED to use a datetime column.

Here's my code :

(birthdate was the column I changed to date....and is one of the few columns where that is possible for me):

//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);

Does someone know a workaround here?

Any help here is greatly appreciated.

解决方案

Its an official bug in Doctrine 2.2, to be resolved in 2.3.

Microseconds are being casted to string with wrong amount of zeros.

Workaround: Change function convertToDatabaseValue in File /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;
}

这篇关于Doctrine 2:无法在SQL Server 2008apm上更新DateTime coloumn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆