Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix QueryBuilder::getParameter() on parameter names with colons #8107

Merged
merged 2 commits into from
Jul 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -602,11 +602,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;
}