Doctrine 2. Unable to update the DateTime column in SQL Server 2008apm.

I am using Doctrine 2.2 with php 5.3 on apache server.

So far I have encountered the following problem: When I try to update the datetime column, I get: SQLSTATE [22007]: [Microsoft] [SQL Server Native Client 10.0] [SQL Server] Conversion failed when converting date and / or time from string characters.

I even went so far as to get into the column, and then use it, adding only 1 day to it to set a new date ...... the same result.

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

My main problem is that there are several fields in which I NEED to use the datetime column.

Here is my code:

(the date of birth was a column that I changed to a date .... and is one of the few columns where this 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 anyone know a workaround here?

+6
source share
3 answers

I ran into this problem in Doctrine 2.5 and SQL Server 2012. The problem is that the database field is of type DATETIME, but doctirne only supports DATETIME2SQLServer2008 and above.

You do not have to edit the files in the directory of your supplier. The correct answer is to create your own type: Doctrine Custom Mapping Types . In my case, I expanded 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 config.yml symfony:

    types:
      datetime: AppBundle\Doctrine\Type\DateTime
+7
source

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;
}
+5

What value do you pass to the field datetime? You must pass an instancedatetime

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

All Articles