Tho*_*mas 7 php datetime doctrine sql-server-2008 doctrine-orm
我在Apache服务器上使用Doctrine 2.2和php 5.3.
到目前为止,我偶然发现了以下问题:当我尝试更新datetime列时,我得到:SQLSTATE [22007]:[Microsoft] [SQL Server Native Client 10.0] [SQL Server]转换日期和/或时间时转换失败来自字符串.
我甚至走到了这么远的地方,然后使用它只用了1天来设置新的日期......相同的结果.
当我改为将数据库中的列和实体从datetime更改为日期时,它按预期运行.
我的主要问题是,有几个字段我需要使用datetime列.
这是我的代码:
(birthdate是我改为日期的专栏......并且是我可能的少数专栏之一):
//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);
Run Code Online (Sandbox Code Playgroud)
有人知道这里的解决方法吗?
我在Doctrine 2.5和SQL Server 2012中遇到了这个问题.问题是数据库字段是类型DATETIME
,但doctirne仅支持DATETIME2
SQLServer2008Platform及更高版本.
您不应该编辑供应商目录中的文件.正确答案是创建自定义类型:Doctrine自定义映射类型.在我的例子中,我扩展了当前的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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在Symfony config.yml中:
types:
datetime: AppBundle\Doctrine\Type\DateTime
Run Code Online (Sandbox Code Playgroud)
它是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 &&
$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;
}
Run Code Online (Sandbox Code Playgroud)
您要向该datetime
领域传递什么价值?你应该传递一个Datetime
实例
$entity->setDate(new \Datetime());
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4801 次 |
最近记录: |