教义2:无法更新SQL Server 2008apm上的DateTime列 [英] Doctrine 2: Can't update DateTime column on SQL Server 2008apm

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

问题描述

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



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



我什至走了很长时间才进入专栏,然后仅添加1天使用它来设置新日期……同样的结果。 p>

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



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



这是我的代码:



(生日是我更改为迄今为止的列...。并且是我可能会使用的少数几列之一):

  //这将返回代表数据库中生日的datetime对象
$ 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.5和SQL Server 2012中遇到了此问题。问题在于数据库字段的类型为 DATETIME ,但doctirne在SQLServer2008Platform及更高版本上仅支持 DATETIME2



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

  <?php 

命名空间AppBundle\教义类型

使用Doctrine\DBAL\Types\DateTimeType;
使用Doctrine\DBAL\Platforms\AbstractPlatform;

类DateTime扩展了DateTimeType
{
private $ dateTimeFormatString =‘Y-m-d H:i:s.000’;

公共函数convertToDatabaseValue($ value,AbstractPlatform $ platform)
{
return($ value!== null)
? $ value-> format($ this-> dateTimeFormatString):null;
}

}

然后在Symfony配置中。 yml:

 类型:
datetime:AppBundle\Doctrine\Type\DateTime


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?

解决方案

I ran into this problem with Doctrine 2.5 and SQL Server 2012. The problem is that the database field is type DATETIME, but doctirne only supports DATETIME2 on SQLServer2008Platform and up.

You should not be editing files in your vendor directory. The correct answer is to create a custom type: Doctrine Custom Mapping Types. In my case, I extended the current 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;
    }

}

And then in the Symfony config.yml:

    types:
      datetime: AppBundle\Doctrine\Type\DateTime

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

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