Skip to content

Instantly share code, notes, and snippets.

@norberttech
Created March 13, 2013 09:37
Show Gist options
  • Save norberttech/5150573 to your computer and use it in GitHub Desktop.
Save norberttech/5150573 to your computer and use it in GitHub Desktop.
Doctrine DateTime2 mapping problem. (MSSQL server will accept it if there will be 3 digits only after dot)
<?php
namespace FSi\Bundle\DemoBundle\Doctrine\DBAL\Types;
use Doctrine\DBAL\Types\VarDateTimeType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class Datetime2Type extends VarDateTimeType
{
public function canRequireSQLConversion()
{
return true;
}
public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
{
return sprintf('CONVERT(datetime2, %s)', $sqlExpr);
}
}
<?php
namespace FSi\Bundle\DemoBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class DemoBundle extends Bundle
{
public function boot()
{
Type::overrideType('datetime', 'FSi\Bundle\DemoBundle\Doctrine\DBAL\Types\Datetime2Type');
}
}
@cenamila
Copy link

cenamila commented Jul 9, 2015

Thanks for the solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment