diff --git a/lib/Doctrine/ORM/AbstractQuery.php b/lib/Doctrine/ORM/AbstractQuery.php index e87813d4377..e0245d44e15 100644 --- a/lib/Doctrine/ORM/AbstractQuery.php +++ b/lib/Doctrine/ORM/AbstractQuery.php @@ -8,6 +8,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Util\ClassUtils; +use Doctrine\DBAL\ArrayParameterType; use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Result; @@ -320,15 +321,16 @@ public function setParameters(ArrayCollection|array $parameters): static /** * Sets a query parameter. * - * @param string|int $key The parameter position or name. - * @param mixed $value The parameter value. - * @param ParameterType|string|int|null $type The parameter type. If specified, the given value will be run through - * the type conversion of this type. This is usually not needed for - * strings and numeric types. + * @param string|int $key The parameter position or name. + * @param mixed $value The parameter value. + * @param ParameterType|ArrayParameterType|string|int|null $type The parameter type. If specified, the given value + * will be run through the type conversion of this + * type. This is usually not needed for strings and + * numeric types. * * @return $this */ - public function setParameter(string|int $key, mixed $value, ParameterType|string|int|null $type = null): static + public function setParameter(string|int $key, mixed $value, ParameterType|ArrayParameterType|string|int|null $type = null): static { $existingParameter = $this->getParameter($key); diff --git a/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php b/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php index 1565bc6f3f8..6b8f883f418 100644 --- a/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php @@ -5,6 +5,7 @@ namespace Doctrine\Tests\ORM\Functional; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\DBAL\ArrayParameterType; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\Type as DBALType; use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; @@ -72,6 +73,43 @@ public function testBasicNativeQuery(): void self::assertEquals('Roman', $users[0]->name); } + public function testNativeQueryWithArrayParameter(): void + { + $user = new CmsUser(); + $user->name = 'William Shatner'; + $user->username = 'wshatner'; + $user->status = 'dev'; + $this->_em->persist($user); + $user = new CmsUser(); + $user->name = 'Leonard Nimoy'; + $user->username = 'lnimoy'; + $user->status = 'dev'; + $this->_em->persist($user); + $user = new CmsUser(); + $user->name = 'DeForest Kelly'; + $user->username = 'dkelly'; + $user->status = 'dev'; + $this->_em->persist($user); + $this->_em->flush(); + + $this->_em->clear(); + + $rsm = new ResultSetMapping(); + $rsm->addEntityResult(CmsUser::class, 'u'); + $rsm->addFieldResult('u', $this->getSQLResultCasing($this->platform, 'id'), 'id'); + $rsm->addFieldResult('u', $this->getSQLResultCasing($this->platform, 'name'), 'name'); + + $query = $this->_em->createNativeQuery('SELECT id, name FROM cms_users WHERE username IN (?) ORDER BY username', $rsm); + $query->setParameter(1, ['wshatner', 'lnimoy'], ArrayParameterType::STRING); + + $users = $query->getResult(); + + self::assertCount(2, $users); + self::assertInstanceOf(CmsUser::class, $users[0]); + self::assertEquals('Leonard Nimoy', $users[0]->name); + self::assertEquals('William Shatner', $users[1]->name); + } + public function testBasicNativeQueryWithMetaResult(): void { $user = new CmsUser(); diff --git a/tests/Doctrine/Tests/ORM/Query/QueryTest.php b/tests/Doctrine/Tests/ORM/Query/QueryTest.php index b04621af004..d24d9292ca9 100644 --- a/tests/Doctrine/Tests/ORM/Query/QueryTest.php +++ b/tests/Doctrine/Tests/ORM/Query/QueryTest.php @@ -7,6 +7,7 @@ use DateTime; use DateTimeImmutable; use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\DBAL\ArrayParameterType; use Doctrine\DBAL\Cache\ArrayResult; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver; @@ -194,6 +195,26 @@ public function testCollectionParameters(): void self::assertEquals($cities, $parameter->getValue()); } + #[Group('DDC-1697')] + public function testExplicitCollectionParameters(): void + { + $cities = [ + 0 => 'Paris', + 3 => 'Cannes', + 9 => 'St Julien', + ]; + + $query = $this->entityManager + ->createQuery('SELECT a FROM Doctrine\Tests\Models\CMS\CmsAddress a WHERE a.city IN (:cities)') + ->setParameter('cities', $cities, ArrayParameterType::STRING); + + $parameters = $query->getParameters(); + $parameter = $parameters->first(); + + self::assertEquals('cities', $parameter->getName()); + self::assertEquals($cities, $parameter->getValue()); + } + /** @psalm-return Generator */ public static function provideProcessParameterValueIterable(): Generator {