Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infer datetime_immutable DBAL type for \DateTimeImmutable instance parameters #8328

Merged
merged 2 commits into from
Dec 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/Doctrine/ORM/Query/ParameterTypeInferer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

namespace Doctrine\ORM\Query;

use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\Type;

Expand Down Expand Up @@ -53,7 +55,11 @@ public static function inferType($value)
return Type::BOOLEAN;
}

if ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
if ($value instanceof DateTimeImmutable) {
return Type::DATETIME_IMMUTABLE;
}

if ($value instanceof DateTimeInterface) {
return Type::DATETIME;
}

Expand Down
6 changes: 2 additions & 4 deletions tests/Doctrine/Tests/ORM/Query/ParameterTypeInfererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\Tests\ORM\Query;

use DateTimeImmutable;
use Doctrine\ORM\Query\ParameterTypeInferer;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Types\Type;
Expand All @@ -18,6 +19,7 @@ public function providerParameterTypeInferer()
["bar", PDO::PARAM_STR],
["1", PDO::PARAM_STR],
[new \DateTime, Type::DATETIME],
[new DateTimeImmutable(), Type::DATETIME_IMMUTABLE],
[new \DateInterval('P1D'), Type::DATEINTERVAL],
[[2], Connection::PARAM_INT_ARRAY],
[["foo"], Connection::PARAM_STR_ARRAY],
Expand All @@ -26,10 +28,6 @@ public function providerParameterTypeInferer()
[true, Type::BOOLEAN],
];

if (PHP_VERSION_ID >= 50500) {
$data[] = [new \DateTimeImmutable(), Type::DATETIME];
}

return $data;
}

Expand Down