Skip to content

Commit

Permalink
Fix QueryBuilder::getParameter() on parameter names with colons (#8107)
Browse files Browse the repository at this point in the history
* Fix type errors

(partially cherry picked from commit 17e7c2a)

* Fix QueryBuilder::getParameter() on parameter names with colons

Fixes #8106.

Co-authored-by: Michael Moravec <mail@majkl578.cz>
  • Loading branch information
tom93 and Majkl578 authored Jul 5, 2020
1 parent 75fe18e commit 3689b76
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/Doctrine/ORM/AbstractQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,13 @@ public function getParameters()
*/
public function getParameter($key)
{
$key = Query\Parameter::normalizeName($key);

$filteredParameters = $this->parameters->filter(
function (Query\Parameter $parameter) use ($key) : bool {
$parameterName = $parameter->getName();

return $key === $parameterName || (string) $key === (string) $parameterName;
return $key === $parameterName;
}
);

Expand Down
14 changes: 13 additions & 1 deletion lib/Doctrine/ORM/Query/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
*/
class Parameter
{
/**
* Returns the internal representation of a parameter name.
*
* @param string|int $name The parameter name or position.
*
* @return string The normalized parameter name.
*/
public static function normalizeName($name)
{
return trim((string) $name, ':');
}

/**
* The parameter name.
*
Expand Down Expand Up @@ -67,7 +79,7 @@ class Parameter
*/
public function __construct($name, $value, $type = null)
{
$this->name = trim($name, ':');
$this->name = self::normalizeName($name);
$this->typeSpecified = $type !== null;

$this->setValue($value, $type);
Expand Down
4 changes: 3 additions & 1 deletion lib/Doctrine/ORM/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,11 +609,13 @@ public function getParameters()
*/
public function getParameter($key)
{
$key = Query\Parameter::normalizeName($key);

$filteredParameters = $this->parameters->filter(
function (Query\Parameter $parameter) use ($key) : bool {
$parameterName = $parameter->getName();

return $key === $parameterName || (string) $key === (string) $parameterName;
return $key === $parameterName;
}
);

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

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Tests\OrmFunctionalTestCase;

/**
* @group GH-8106
*/
class GH8106Test extends OrmFunctionalTestCase
{
/**
* {@inheritDoc}
*/
protected function setUp() : void
{
parent::setUp();

$this->_schemaTool->createSchema(
[
$this->_em->getClassMetadata(GH8106User::class),
]
);
}

public function testIssue() : void
{
$user = new GH8106User();
$this->_em->persist($user);
$this->_em->flush();
$this->_em->clear();

$qb = $this->_em->createQueryBuilder();
$qb
->select('u')
->from(GH8106User::class, 'u')
->where('u.id = :id')
->setParameter(':id', 1)
->setParameter(':id', 1);

$result = $qb->getQuery()->getResult(); // should not throw QueryException

self::assertCount(1, $result);
}
}

/** @Entity */
class GH8106User
{
/** @Id @Column(type="integer") @GeneratedValue */
public $id;
}

0 comments on commit 3689b76

Please sign in to comment.