Skip to content

Instantly share code, notes, and snippets.

@jenkoian
Last active December 21, 2015 04:29
Show Gist options
  • Save jenkoian/6249513 to your computer and use it in GitHub Desktop.
Save jenkoian/6249513 to your computer and use it in GitHub Desktop.
Doctrine2 DateTimeType to work around SQLServerPlatform compatibility issues between 2005/2008
<?php
namespace Acme\Bundle\DoctrineExtensions\DBAL\Types;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Types\DateTimeType as BaseDateTimeType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
class DateTimeType extends BaseDateTimeType
{
/**
* @var string
*/
const DB_FORMAT = 'Y-m-d H:i:s.000';
/**
* Gets around a problem when saving data in SQLServer2008 mode and SQLServer2005 mode by using a common format
* for all SQLServer platforms.
*
* @param mixed $value
* @param AbstractPlatform $platform
* @return null
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (null === $value) {
return null;
}
// Use unified format for all SQLServer platforms when saving to database
if ($platform instanceof SQLServerPlatform) {
return $value->format(self::DB_FORMAT);
}
return parent::convertToDatabaseValue($value, $platform);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment