Skip to content

Instantly share code, notes, and snippets.

@CarsonF
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarsonF/1d7b60058eb21fd4301f to your computer and use it in GitHub Desktop.
Save CarsonF/1d7b60058eb21fd4301f to your computer and use it in GitHub Desktop.
Doctrine DBAL Carbon Integration
<?php
use Carbon\Carbon;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\DateTimeType;
/**
* Updates DateTime objects to return Carbon instance
*/
class CarbonType extends DateTimeType
{
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}
return Carbon::instance(parent::convertToPHPValue($value, $platform));
}
public static function override()
{
static::overrideType(static::DATETIME, get_called_class());
}
}
<?php
use Carbon\Carbon;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\DateTimeTzType;
/**
* Updates DateTime objects to return Carbon instance
*/
class CarbonTzType extends DateTimeTzType
{
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}
return Carbon::instance(parent::convertToPHPValue($value, $platform));
}
public static function override()
{
static::overrideType(static::DATETIMETZ, get_called_class());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment