1
0
mirror of synced 2025-02-09 08:49:26 +03:00

Update DateTime cookbook code

It seems that existing code was outdated and lacked ``use`` statement in order to work correctly.
We also added instructions on how to configure the new type, since it may not be straightforward for newcomers.
This commit is contained in:
Adrien Crivelli 2015-08-27 15:39:26 +09:00
parent f88896cc9d
commit 93806a8036

View File

@ -85,43 +85,60 @@ the UTC time at the time of the booking and the timezone the event happened in.
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException; use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateTimeType;
class UTCDateTimeType extends DateTimeType class UTCDateTimeType extends DateTimeType
{ {
static private $utc = null; static private $utc = null;
public function convertToDatabaseValue($value, AbstractPlatform $platform) private static function getUtc()
{ {
if ($value === null) { if (!self::$utc) {
return null; self::$utc = new \DateTimeZone('UTC');
} }
return self::$utc;
}
return $value->format($platform->getDateTimeFormatString(), public function convertToDatabaseValue($value, AbstractPlatform $platform)
(self::$utc) ? self::$utc : (self::$utc = new \DateTimeZone('UTC')) {
); if ($value) {
$value->setTimezone(self::getUtc());
}
return parent::convertToDatabaseValue($value, $platform);
} }
public function convertToPHPValue($value, AbstractPlatform $platform) public function convertToPHPValue($value, AbstractPlatform $platform)
{ {
if ($value === null) { if ($value === null || $value instanceof \DateTime) {
return null; return $value;
} }
$val = \DateTime::createFromFormat( $val = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value, self::getUtc());
$platform->getDateTimeFormatString(),
$value,
(self::$utc) ? self::$utc : (self::$utc = new \DateTimeZone('UTC'))
);
if (!$val) { if (!$val) {
throw ConversionException::conversionFailed($value, $this->getName()); throw ConversionException::conversionFailedFormat($value, $this->getName(), $platform->getDateTimeFormatString());
} }
return $val; return $val;
} }
} }
This database type makes sure that every DateTime instance is always saved in UTC, relative This database type makes sure that every DateTime instance is always saved in UTC, relative
to the current timezone that the passed DateTime instance has. To be able to transform these values to the current timezone that the passed DateTime instance has. To actually use that new type, configure it in ``config.yml`` like so:
.. code-block:: yml
doctrine:
dbal:
types:
datetime: DoctrineExtensions\DBAL\Types\UTCDateTimeType
datetimetz: DoctrineExtensions\DBAL\Types\UTCDateTimeType
To be able to transform these values
back into their real timezone you have to save the timezone in a separate field of the entity back into their real timezone you have to save the timezone in a separate field of the entity
requiring timezoned datetimes: requiring timezoned datetimes: