Skip to content

Commit

Permalink
Fix array parameters with DBAL 4 (#10995)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus committed Oct 11, 2023
1 parent 7827453 commit 06eb00d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/Doctrine/ORM/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
38 changes: 38 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
21 changes: 21 additions & 0 deletions tests/Doctrine/Tests/ORM/Query/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, array{iterable}> */
public static function provideProcessParameterValueIterable(): Generator
{
Expand Down

0 comments on commit 06eb00d

Please sign in to comment.