Last active
July 19, 2021 21:18
-
-
Save SerginhoLD/695607878dbf9df913c7afdbf92bae95 to your computer and use it in GitHub Desktop.
Doctrine datetime with ms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App\Doctrine\Types; | |
use Doctrine\DBAL\Platforms\AbstractPlatform; | |
use Doctrine\DBAL\Types\ConversionException; | |
use Doctrine\DBAL\Types\DateTimeType; | |
use Doctrine\DBAL\Types\PhpIntegerMappingType; | |
/** | |
* todo: PhpIntegerMappingType костыль для DEFAULT CURRENT_TIMESTAMP(6) | |
* @see \Doctrine\DBAL\Platforms\MySqlPlatform::getAlterTableSQL | |
* @see \Doctrine\DBAL\Platforms\AbstractPlatform::getColumnDeclarationSQL | |
* @see \Doctrine\DBAL\Platforms\AbstractPlatform::getDefaultValueDeclarationSQL | |
*/ | |
class DateTimeImmutableMs extends DateTimeType implements PhpIntegerMappingType | |
{ | |
const DATETIME_IMMUTABLE_MS = 'datetime_immutable_ms'; | |
private const FORMAT = 'Y-m-d H:i:s.u'; | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getName(): string | |
{ | |
return self::DATETIME_IMMUTABLE_MS; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string | |
{ | |
$precision = $column['precision'] > 6 ? 6 : $column['precision']; | |
//if (isset($column['version']) && $column['version'] === true) { | |
// return "TIMESTAMP({$precision})"; | |
//} | |
return "DATETIME({$precision})"; | |
} | |
/** | |
* {@inheritdoc} | |
* @throws ConversionException | |
*/ | |
public function convertToDatabaseValue($value, AbstractPlatform $platform) | |
{ | |
if ($value === null) | |
return null; | |
if ($value instanceof \DateTimeImmutable) | |
return $value->format(self::FORMAT); | |
throw ConversionException::conversionFailedInvalidType( | |
$value, | |
$this->getName(), | |
['null', \DateTimeImmutable::class] | |
); | |
} | |
/** | |
* {@inheritdoc} | |
* @throws ConversionException | |
*/ | |
public function convertToPHPValue($value, AbstractPlatform $platform) | |
{ | |
if ($value === null || $value instanceof \DateTimeImmutable) | |
return $value; | |
$dateTime = \DateTimeImmutable::createFromFormat(self::FORMAT, $value); | |
if (!$dateTime) { | |
$dateTime = date_create_immutable($value); | |
} | |
if ($dateTime) | |
return $dateTime; | |
throw ConversionException::conversionFailedFormat( | |
$value, | |
$this->getName(), | |
self::FORMAT | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function requiresSQLCommentHint(AbstractPlatform $platform): bool | |
{ | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
doctrine: | |
dbal: | |
types: | |
datetime_immutable_ms: App\Doctrine\Types\DateTimeImmutableMs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Doctrine\ORM\Mapping as ORM; | |
class Entity | |
{ | |
#[ORM\Column(type: 'datetime_immutable_ms', precision: 6, options: ['default' => 'CURRENT_TIMESTAMP(6)'])] | |
private ?\DateTimeImmutable $created_at; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment