Skip to content

Commit

Permalink
Merge pull request #11543 from stof/fix_native_query_parameter_type
Browse files Browse the repository at this point in the history
Fix the support for custom parameter types in native queries
  • Loading branch information
greg0ire authored Jul 4, 2024
2 parents c37b115 + 9bd51aa commit 51ad860
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/NativeQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ protected function _doExecute()
$types = [];

foreach ($this->getParameters() as $parameter) {
$name = $parameter->getName();
$name = $parameter->getName();

if ($parameter->typeWasSpecified()) {
$parameters[$name] = $parameter->getValue();
$types[$name] = $parameter->getType();

continue;
}

$value = $this->processParameterValue($parameter->getValue());
$type = $parameter->getValue() === $value
? $parameter->getType()
Expand Down
42 changes: 42 additions & 0 deletions tests/Tests/ORM/Query/NativeQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Query;

use DateTime;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Tests\Mocks\EntityManagerMock;
use Doctrine\Tests\OrmTestCase;

class NativeQueryTest extends OrmTestCase
{
/** @var EntityManagerMock */
protected $entityManager;

protected function setUp(): void
{
$this->entityManager = $this->getTestEntityManager();
}

public function testValuesAreNotBeingResolvedForSpecifiedParameterTypes(): void
{
$unitOfWork = $this->createMock(UnitOfWork::class);

$this->entityManager->setUnitOfWork($unitOfWork);

$unitOfWork
->expects(self::never())
->method('getSingleIdentifierValue');

$rsm = new ResultSetMapping();

$query = $this->entityManager->createNativeQuery('SELECT d.* FROM date_time_model d WHERE d.datetime = :value', $rsm);

$query->setParameter('value', new DateTime(), Types::DATETIME_MUTABLE);

self::assertEmpty($query->getResult());
}
}

0 comments on commit 51ad860

Please sign in to comment.