From 3eace16e8533e3846d9c7dbc7809fc8c487600f7 Mon Sep 17 00:00:00 2001 From: Hanish Singla Date: Tue, 20 Feb 2024 13:36:20 +0530 Subject: [PATCH 1/2] Allow (Array)ParameterType in QueryBuilder --- src/QueryBuilder.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 115dea0187d..f03951d2092 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -6,6 +6,8 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Criteria; +use Doctrine\DBAL\ArrayParameterType; +use Doctrine\DBAL\ParameterType; use Doctrine\ORM\Internal\QueryType; use Doctrine\ORM\Query\Expr; use Doctrine\ORM\Query\Parameter; @@ -428,12 +430,12 @@ public function getRootEntities(): array * ->setParameter('user_id', 1); * * - * @param string|int $key The parameter position or name. - * @param string|int|null $type ParameterType::* or \Doctrine\DBAL\Types\Type::* constant + * @param string|int $key The parameter position or name. + * @param ParameterType|ArrayParameterType|string|int|null $type ParameterType::*, ArrayParameterType::* or \Doctrine\DBAL\Types\Type::* constant * * @return $this */ - public function setParameter(string|int $key, mixed $value, 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); From 708146bbbc08651d8d2ec4eaa144b3ce96a57cf7 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Thu, 22 Feb 2024 00:00:19 +0100 Subject: [PATCH 2/2] Test different ways of settings query parameters --- .../ORM/Functional/QueryParameterTest.php | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 tests/Tests/ORM/Functional/QueryParameterTest.php diff --git a/tests/Tests/ORM/Functional/QueryParameterTest.php b/tests/Tests/ORM/Functional/QueryParameterTest.php new file mode 100644 index 00000000000..e7599cea5ea --- /dev/null +++ b/tests/Tests/ORM/Functional/QueryParameterTest.php @@ -0,0 +1,124 @@ +useModelSet('cms'); + + parent::setUp(); + + $user = new CmsUser(); + $user->name = 'John Doe'; + $user->username = 'john'; + $user2 = new CmsUser(); + $user2->name = 'Jane Doe'; + $user2->username = 'jane'; + $user3 = new CmsUser(); + $user3->name = 'Just Bill'; + $user3->username = 'bill'; + + $this->_em->persist($user); + $this->_em->persist($user2); + $this->_em->persist($user3); + $this->_em->flush(); + + $this->userId = $user->id; + + $this->_em->clear(); + } + + public function testParameterTypeInBuilder(): void + { + $result = $this->_em->createQueryBuilder() + ->from(CmsUser::class, 'u') + ->select('u.name') + ->where('u.id = :id') + ->setParameter('id', $this->userId, ParameterType::INTEGER) + ->getQuery() + ->getArrayResult(); + + self::assertSame([['name' => 'John Doe']], $result); + } + + public function testParameterTypeInQuery(): void + { + $result = $this->_em->createQueryBuilder() + ->from(CmsUser::class, 'u') + ->select('u.name') + ->where('u.id = :id') + ->getQuery() + ->setParameter('id', $this->userId, ParameterType::INTEGER) + ->getArrayResult(); + + self::assertSame([['name' => 'John Doe']], $result); + } + + public function testDbalTypeStringInBuilder(): void + { + $result = $this->_em->createQueryBuilder() + ->from(CmsUser::class, 'u') + ->select('u.name') + ->where('u.id = :id') + ->setParameter('id', $this->userId, Types::INTEGER) + ->getQuery() + ->getArrayResult(); + + self::assertSame([['name' => 'John Doe']], $result); + } + + public function testDbalTypeStringInQuery(): void + { + $result = $this->_em->createQueryBuilder() + ->from(CmsUser::class, 'u') + ->select('u.name') + ->where('u.id = :id') + ->getQuery() + ->setParameter('id', $this->userId, Types::INTEGER) + ->getArrayResult(); + + self::assertSame([['name' => 'John Doe']], $result); + } + + public function testArrayParameterTypeInBuilder(): void + { + $result = $this->_em->createQueryBuilder() + ->from(CmsUser::class, 'u') + ->select('u.name') + ->where('u.username IN (:usernames)') + ->orderBy('u.username') + ->setParameter('usernames', ['john', 'jane'], ArrayParameterType::STRING) + ->getQuery() + ->getArrayResult(); + + self::assertSame([['name' => 'Jane Doe'], ['name' => 'John Doe']], $result); + } + + public function testArrayParameterTypeInQuery(): void + { + $result = $this->_em->createQueryBuilder() + ->from(CmsUser::class, 'u') + ->select('u.name') + ->where('u.username IN (:usernames)') + ->orderBy('u.username') + ->getQuery() + ->setParameter('usernames', ['john', 'jane'], ArrayParameterType::STRING) + ->getArrayResult(); + + self::assertSame([['name' => 'Jane Doe'], ['name' => 'John Doe']], $result); + } +}