Skip to content

Commit

Permalink
Merge pull request #4314 from BacLuc/fix-datetimeimmutable-deprecation
Browse files Browse the repository at this point in the history
api: fix "Passing an instance of DateTimeImmutable is deprecated"
  • Loading branch information
usu committed Dec 25, 2023
2 parents c11bb35 + 042bc4c commit 9754730
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api/src/Types/Doctrine/UTCDateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateTimeImmutableType;
use Doctrine\DBAL\Types\DateTimeType;

/**
Expand Down Expand Up @@ -38,6 +39,10 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
if ($value instanceof \DateTime || $value instanceof \DateTimeImmutable) {
$value = $value->setTimeZone(self::getUtc());

if ($value instanceof \DateTimeImmutable) {
return (new DateTimeImmutableType())->convertToDatabaseValue($value, $platform);
}

return parent::convertToDatabaseValue($value, $platform);
}

Expand Down
5 changes: 5 additions & 0 deletions api/src/Types/Doctrine/UTCDateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateImmutableType;
use Doctrine\DBAL\Types\DateType;

class UTCDateType extends DateType {
Expand All @@ -23,6 +24,10 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
$value = $value->setTimezone(self::getUtc());
}

if ($value instanceof \DateTimeImmutable) {
return (new DateImmutableType())->convertToDatabaseValue($value, $platform);
}

return parent::convertToDatabaseValue($value, $platform);
}

Expand Down

0 comments on commit 9754730

Please sign in to comment.