Skip to content

Commit

Permalink
Merge pull request #11287 from derrabus/bugfix/parameter-types
Browse files Browse the repository at this point in the history
Allow (Array)ParameterType in QueryBuilder
  • Loading branch information
derrabus committed Feb 22, 2024
2 parents a5bf9bb + 708146b commit 44fa5d3
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -428,12 +430,12 @@ public function getRootEntities(): array
* ->setParameter('user_id', 1);
* </code>
*
* @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);

Expand Down
124 changes: 124 additions & 0 deletions tests/Tests/ORM/Functional/QueryParameterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\Types;
use Doctrine\Tests\Models\CMS\CmsUser;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;

#[Group('GH-11278')]
final class QueryParameterTest extends OrmFunctionalTestCase
{
private int $userId;

protected function setUp(): void
{
$this->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);
}
}

0 comments on commit 44fa5d3

Please sign in to comment.