Skip to content
Open
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
42 changes: 42 additions & 0 deletions src/Utility/PersisterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Doctrine\ORM\Utility;

use BackedEnum;
use DateTimeInterface;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver;
Expand All @@ -17,7 +19,9 @@
use function array_map;
use function array_merge;
use function assert;
use function in_array;
use function is_array;
use function is_string;
use function is_object;
use function sprintf;

Expand All @@ -29,6 +33,17 @@
*/
class PersisterHelper
{
private const DATE_TIME_TYPES = [
Types::DATE_MUTABLE,
Types::DATE_IMMUTABLE,
Types::DATETIME_MUTABLE,
Types::DATETIME_IMMUTABLE,
Types::DATETIMETZ_MUTABLE,
Types::DATETIMETZ_IMMUTABLE,
Types::TIME_MUTABLE,
Types::TIME_IMMUTABLE,
];

/**
* @return list<string>
*
Expand Down Expand Up @@ -130,6 +145,7 @@ public static function inferParameterTypes(
ClassMetadata $class,
EntityManagerInterface $em,
): array {
/** @var list<ParameterType::*|string> $types */
$types = [];

switch (true) {
Expand Down Expand Up @@ -159,13 +175,39 @@ public static function inferParameterTypes(
break;
}

$types = self::normalizeDateTimeTypesForValue($types, $value);

if (is_array($value)) {
return array_map(self::getArrayBindingType(...), $types);
}

return $types;
}

/**
* @param list<ParameterType::*|string> $types
*
* @return list<ParameterType::*|string>
*/
private static function normalizeDateTimeTypesForValue(array $types, mixed $value): array
{
if ($value instanceof DateTimeInterface) {
return $types;
}

if (! is_string($value)) {
return $types;
}

foreach ($types as $index => $type) {
if (is_string($type) && in_array($type, self::DATE_TIME_TYPES, true)) {
$types[$index] = ParameterType::STRING;
}
}

return $types;
}

/** @phpstan-return ArrayParameterType::* */
private static function getArrayBindingType(ParameterType|int|string $type): ArrayParameterType|int
{
Expand Down
32 changes: 32 additions & 0 deletions tests/Tests/ORM/Functional/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,36 @@ public function testTime(): void
self::assertInstanceOf(DateTime::class, $dateTimeDb->time);
self::assertSame('19:27:20', $dateTimeDb->time->format('H:i:s'));
}

public function testFindByDateWithString(): void
{
$dateTime = new DateTimeModel();
$dateTime->date = new DateTime('2009-10-01', new DateTimeZone('Europe/Berlin'));

$this->_em->persist($dateTime);
$this->_em->flush();
$this->_em->clear();

$found = $this->_em->getRepository(DateTimeModel::class)
->findBy(['date' => '2009-10-01']);

self::assertCount(1, $found);
self::assertSame('2009-10-01', $found[0]->date->format('Y-m-d'));
}

public function testFindByTimeWithString(): void
{
$dateTime = new DateTimeModel();
$dateTime->time = new DateTime('2010-01-01 19:27:20');

$this->_em->persist($dateTime);
$this->_em->flush();
$this->_em->clear();

$found = $this->_em->getRepository(DateTimeModel::class)
->findBy(['time' => '19:27:20']);

self::assertCount(1, $found);
self::assertSame('19:27:20', $found[0]->time->format('H:i:s'));
}
}
Loading