Skip to content

Commit 44fa5d3

Browse files
authored
Merge pull request #11287 from derrabus/bugfix/parameter-types
Allow (Array)ParameterType in QueryBuilder
2 parents a5bf9bb + 708146b commit 44fa5d3

File tree

2 files changed

+129
-3
lines changed

2 files changed

+129
-3
lines changed

src/QueryBuilder.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use Doctrine\Common\Collections\ArrayCollection;
88
use Doctrine\Common\Collections\Criteria;
9+
use Doctrine\DBAL\ArrayParameterType;
10+
use Doctrine\DBAL\ParameterType;
911
use Doctrine\ORM\Internal\QueryType;
1012
use Doctrine\ORM\Query\Expr;
1113
use Doctrine\ORM\Query\Parameter;
@@ -428,12 +430,12 @@ public function getRootEntities(): array
428430
* ->setParameter('user_id', 1);
429431
* </code>
430432
*
431-
* @param string|int $key The parameter position or name.
432-
* @param string|int|null $type ParameterType::* or \Doctrine\DBAL\Types\Type::* constant
433+
* @param string|int $key The parameter position or name.
434+
* @param ParameterType|ArrayParameterType|string|int|null $type ParameterType::*, ArrayParameterType::* or \Doctrine\DBAL\Types\Type::* constant
433435
*
434436
* @return $this
435437
*/
436-
public function setParameter(string|int $key, mixed $value, string|int|null $type = null): static
438+
public function setParameter(string|int $key, mixed $value, ParameterType|ArrayParameterType|string|int|null $type = null): static
437439
{
438440
$existingParameter = $this->getParameter($key);
439441

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\ORM\Functional;
6+
7+
use Doctrine\DBAL\ArrayParameterType;
8+
use Doctrine\DBAL\ParameterType;
9+
use Doctrine\DBAL\Types\Types;
10+
use Doctrine\Tests\Models\CMS\CmsUser;
11+
use Doctrine\Tests\OrmFunctionalTestCase;
12+
use PHPUnit\Framework\Attributes\Group;
13+
14+
#[Group('GH-11278')]
15+
final class QueryParameterTest extends OrmFunctionalTestCase
16+
{
17+
private int $userId;
18+
19+
protected function setUp(): void
20+
{
21+
$this->useModelSet('cms');
22+
23+
parent::setUp();
24+
25+
$user = new CmsUser();
26+
$user->name = 'John Doe';
27+
$user->username = 'john';
28+
$user2 = new CmsUser();
29+
$user2->name = 'Jane Doe';
30+
$user2->username = 'jane';
31+
$user3 = new CmsUser();
32+
$user3->name = 'Just Bill';
33+
$user3->username = 'bill';
34+
35+
$this->_em->persist($user);
36+
$this->_em->persist($user2);
37+
$this->_em->persist($user3);
38+
$this->_em->flush();
39+
40+
$this->userId = $user->id;
41+
42+
$this->_em->clear();
43+
}
44+
45+
public function testParameterTypeInBuilder(): void
46+
{
47+
$result = $this->_em->createQueryBuilder()
48+
->from(CmsUser::class, 'u')
49+
->select('u.name')
50+
->where('u.id = :id')
51+
->setParameter('id', $this->userId, ParameterType::INTEGER)
52+
->getQuery()
53+
->getArrayResult();
54+
55+
self::assertSame([['name' => 'John Doe']], $result);
56+
}
57+
58+
public function testParameterTypeInQuery(): void
59+
{
60+
$result = $this->_em->createQueryBuilder()
61+
->from(CmsUser::class, 'u')
62+
->select('u.name')
63+
->where('u.id = :id')
64+
->getQuery()
65+
->setParameter('id', $this->userId, ParameterType::INTEGER)
66+
->getArrayResult();
67+
68+
self::assertSame([['name' => 'John Doe']], $result);
69+
}
70+
71+
public function testDbalTypeStringInBuilder(): void
72+
{
73+
$result = $this->_em->createQueryBuilder()
74+
->from(CmsUser::class, 'u')
75+
->select('u.name')
76+
->where('u.id = :id')
77+
->setParameter('id', $this->userId, Types::INTEGER)
78+
->getQuery()
79+
->getArrayResult();
80+
81+
self::assertSame([['name' => 'John Doe']], $result);
82+
}
83+
84+
public function testDbalTypeStringInQuery(): void
85+
{
86+
$result = $this->_em->createQueryBuilder()
87+
->from(CmsUser::class, 'u')
88+
->select('u.name')
89+
->where('u.id = :id')
90+
->getQuery()
91+
->setParameter('id', $this->userId, Types::INTEGER)
92+
->getArrayResult();
93+
94+
self::assertSame([['name' => 'John Doe']], $result);
95+
}
96+
97+
public function testArrayParameterTypeInBuilder(): void
98+
{
99+
$result = $this->_em->createQueryBuilder()
100+
->from(CmsUser::class, 'u')
101+
->select('u.name')
102+
->where('u.username IN (:usernames)')
103+
->orderBy('u.username')
104+
->setParameter('usernames', ['john', 'jane'], ArrayParameterType::STRING)
105+
->getQuery()
106+
->getArrayResult();
107+
108+
self::assertSame([['name' => 'Jane Doe'], ['name' => 'John Doe']], $result);
109+
}
110+
111+
public function testArrayParameterTypeInQuery(): void
112+
{
113+
$result = $this->_em->createQueryBuilder()
114+
->from(CmsUser::class, 'u')
115+
->select('u.name')
116+
->where('u.username IN (:usernames)')
117+
->orderBy('u.username')
118+
->getQuery()
119+
->setParameter('usernames', ['john', 'jane'], ArrayParameterType::STRING)
120+
->getArrayResult();
121+
122+
self::assertSame([['name' => 'Jane Doe'], ['name' => 'John Doe']], $result);
123+
}
124+
}

0 commit comments

Comments
 (0)