DDC-2844: Doctrine\ORM\Query\QueryException raised when using Criteria object with more than one condition in repository. #3550

Closed
opened 2026-01-22 14:22:10 +01:00 by admin · 14 comments
Owner

Originally created by @doctrinebot on GitHub (Dec 7, 2013).

Originally assigned to: @Ocramius on GitHub.

Jira issue originally created by user yanakey:

$userRepository = $em->getRepository ('User\Entity\User');
$date = '2013-12-02';
$dateFrom = new \DateTime ($date);
$fateTo = (new \DateTime ($date))->add (new \DateInterval ('P1D');

// OK
$criteria = new \Doctrine\Common\Collections\Criteria ();
$criteria
    ->where ($criteria->expr ()->gte ('signedIn', $dateFrom))
    ->andWhere ($criteria->expr ()->lt ('signedIn', $dateTo));
var_dump ($userRepository->matching ($criteria));


// NOT OK - Doctrine\ORM\Query\QueryException "Invalid parameter number: number of bound variables does not match number of tokens"
$criteria = new \Doctrine\Common\Collections\Criteria ();
$criteria
    ->where ($criteria->expr ()->gte ('user.signedIn', $dateFrom))
    ->andWhere ($criteria->expr ()->lt ('user.signedIn', $dateTo));
var_dump ($userRepository->createQueryBuilder ('user')->addCriteria ($criteria)->getQuery ()->getResult ());


// NOT OK - Doctrine\ORM\Query\QueryException "Invalid parameter number: number of bound variables does not match number of tokens"
$criteria
    ->where ($criteria->expr ()->eq ('user.active', true))
    ->andWhere ($criteria->expr ()->eq ('user.active', true));
    //->orWhere ($criteria->expr ()->eq ('user.active', true));
var_dump ($userRepository->createQueryBuilder ('user')->addCriteria ($criteria)->getQuery ()->getResult ());
Originally created by @doctrinebot on GitHub (Dec 7, 2013). Originally assigned to: @Ocramius on GitHub. Jira issue originally created by user yanakey: ``` $userRepository = $em->getRepository ('User\Entity\User'); $date = '2013-12-02'; $dateFrom = new \DateTime ($date); $fateTo = (new \DateTime ($date))->add (new \DateInterval ('P1D'); // OK $criteria = new \Doctrine\Common\Collections\Criteria (); $criteria ->where ($criteria->expr ()->gte ('signedIn', $dateFrom)) ->andWhere ($criteria->expr ()->lt ('signedIn', $dateTo)); var_dump ($userRepository->matching ($criteria)); // NOT OK - Doctrine\ORM\Query\QueryException "Invalid parameter number: number of bound variables does not match number of tokens" $criteria = new \Doctrine\Common\Collections\Criteria (); $criteria ->where ($criteria->expr ()->gte ('user.signedIn', $dateFrom)) ->andWhere ($criteria->expr ()->lt ('user.signedIn', $dateTo)); var_dump ($userRepository->createQueryBuilder ('user')->addCriteria ($criteria)->getQuery ()->getResult ()); // NOT OK - Doctrine\ORM\Query\QueryException "Invalid parameter number: number of bound variables does not match number of tokens" $criteria ->where ($criteria->expr ()->eq ('user.active', true)) ->andWhere ($criteria->expr ()->eq ('user.active', true)); //->orWhere ($criteria->expr ()->eq ('user.active', true)); var_dump ($userRepository->createQueryBuilder ('user')->addCriteria ($criteria)->getQuery ()->getResult ()); ```
admin added the Bug label 2026-01-22 14:22:10 +01:00
admin closed this issue 2026-01-22 14:22:10 +01:00
Author
Owner

@doctrinebot commented on GitHub (Dec 14, 2013):

Comment created by cordoval:

so I have reproduced the problem https://github.com/cordoval/symfony-standard/pull/8/files

what i think is happening is problem lies on Criteria api not being able to handle well multiple parameters in expression

  private '*dql' => string 'SELECT user FROM Acme\DemoBundle\Entity\User user WHERE user.signedIn >= :user_signedIn AND user.signedIn < :user*signedIn' (length=122)

it tries to figure out a name and fails to name them properly on parameters internal property.

I am working on a fix.

@doctrinebot commented on GitHub (Dec 14, 2013): Comment created by cordoval: so I have reproduced the problem https://github.com/cordoval/symfony-standard/pull/8/files what i think is happening is problem lies on Criteria api not being able to handle well multiple parameters in expression ``` private '*dql' => string 'SELECT user FROM Acme\DemoBundle\Entity\User user WHERE user.signedIn >= :user_signedIn AND user.signedIn < :user*signedIn' (length=122) ``` it tries to figure out a name and fails to name them properly on parameters internal property. I am working on a fix.
Author
Owner

@doctrinebot commented on GitHub (Dec 14, 2013):

Comment created by cordoval:

writing failing test first at QueryBuilderTest::testAddCriteriaWhere() here https://github.com/doctrine/doctrine2/pull/874
now working on seeing why that commit in the description may have not solved these other cases

@doctrinebot commented on GitHub (Dec 14, 2013): Comment created by cordoval: writing failing test first at QueryBuilderTest::testAddCriteriaWhere() here https://github.com/doctrine/doctrine2/pull/874 now working on seeing why that commit in the description may have not solved these other cases
Author
Owner

@doctrinebot commented on GitHub (Dec 14, 2013):

Comment created by cordoval:

this should be closed as it is working on master :)

@doctrinebot commented on GitHub (Dec 14, 2013): Comment created by cordoval: this should be closed as it is working on master :)
Author
Owner

@doctrinebot commented on GitHub (Dec 14, 2013):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-874] was closed:
https://github.com/doctrine/doctrine2/pull/874

@doctrinebot commented on GitHub (Dec 14, 2013): Comment created by @doctrinebot: A related Github Pull-Request [GH-874] was closed: https://github.com/doctrine/doctrine2/pull/874
Author
Owner

@doctrinebot commented on GitHub (Dec 14, 2013):

Comment created by @beberlei:

@doctrinebot commented on GitHub (Dec 14, 2013): Comment created by @beberlei: ##
Author
Owner

@doctrinebot commented on GitHub (Dec 14, 2013):

Comment created by @ocramius:

As [~cordoval] said, this seems to be a non-issue. I provided tests to validate that at https://github.com/doctrine/doctrine2/pull/875

@doctrinebot commented on GitHub (Dec 14, 2013): Comment created by @ocramius: As [~cordoval] said, this seems to be a non-issue. I provided tests to validate that at https://github.com/doctrine/doctrine2/pull/875
Author
Owner

@doctrinebot commented on GitHub (Dec 14, 2013):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-875] was closed:
https://github.com/doctrine/doctrine2/pull/875

@doctrinebot commented on GitHub (Dec 14, 2013): Comment created by @doctrinebot: A related Github Pull-Request [GH-875] was closed: https://github.com/doctrine/doctrine2/pull/875
Author
Owner

@doctrinebot commented on GitHub (Dec 14, 2013):

Comment created by @ocramius:

Tests provided at ce914bef3f

@doctrinebot commented on GitHub (Dec 14, 2013): Comment created by @ocramius: Tests provided at https://github.com/doctrine/doctrine2/commit/ce914bef3fddebb4b54f4353f5daad6241802d9d
Author
Owner

@doctrinebot commented on GitHub (Dec 14, 2013):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Dec 14, 2013): Issue was closed with resolution "Fixed"
Author
Owner

@doctrinebot commented on GitHub (Jun 3, 2014):

Comment created by althaus:

Stumbled upon this issue today. As this is fixed in master, but not in the 2.4.1 or 2.4.2 release... is this expected to be part of another 2.4.x or only 2.5 as it's just a "unsupported case" and no bug? Would be great if someone could updated the "Fix Version/s" as it's definitely not resolved in 2.4.1.

@doctrinebot commented on GitHub (Jun 3, 2014): Comment created by althaus: Stumbled upon this issue today. As this is fixed in master, but not in the 2.4.1 or 2.4.2 release... is this expected to be part of another 2.4.x or only 2.5 as it's just a "unsupported case" and no bug? Would be great if someone could updated the "Fix Version/s" as it's definitely not resolved in 2.4.1.
Author
Owner

@doctrinebot commented on GitHub (Jun 6, 2014):

Comment created by @ocramius:

[~althaus] do the tests in this issue run on 2.4.x?

@doctrinebot commented on GitHub (Jun 6, 2014): Comment created by @ocramius: [~althaus] do the tests in this issue run on 2.4.x?
Author
Owner

@doctrinebot commented on GitHub (Jun 10, 2014):

Comment created by althaus:

[~ocramius], which tests do you mean exactly?

Comparing the Doctrine\ORM\Query\QueryExpressionVisitor of the just released 2.4.3 and master the 2.4.x branch is still missing the fix.

@doctrinebot commented on GitHub (Jun 10, 2014): Comment created by althaus: [~ocramius], which tests do you mean exactly? Comparing the `Doctrine\ORM\Query\QueryExpressionVisitor` of the just released [2.4.3](https://github.com/doctrine/doctrine2/blob/v2.4.3/lib/Doctrine/ORM/Query/QueryExpressionVisitor.php#L135) and [master](https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/Query/QueryExpressionVisitor.php#L132) the 2.4.x branch is still missing the fix.
Author
Owner

@leobedrosian commented on GitHub (Nov 8, 2017):

Just ran into this myself. The problem is with the parameter binding logic in Doctrine\ORM\Query\QueryExpressionVisitor. The walkComparison method does not properly handle parameter naming such that parameters are assigned unique names to which the right values can be bound. Instead, the same parameter names are being used/reused and overwritten with each condition on the same column. This is definitely fixed in the master branch (currently v2.5) but the problem exists in 2.4.

The simplest solution is to add an incremental, numerical suffix to parameter names based on the number of parameters defined so that every single parameter is assured to be unique for value binding purposes. Ex (line 137):

$parameterName = str_replace('.', '_', $comparison->getField()) . count($this->parameters);

In the master branch (currently v2.5), there's some iteration on parameter names happening to resolve this issue (lines 150 to 155) but this seems needlessly inefficient and overkill:

foreach($this->parameters as $parameter) { if($parameter->getName() === $parameterName) { $parameterName .= '_' . count($this->parameters); break; } }

Reference file: https://github.com/doctrine/doctrine2/blob/2.5/lib/Doctrine/ORM/Query/QueryExpressionVisitor.php

@leobedrosian commented on GitHub (Nov 8, 2017): Just ran into this myself. The problem is with the parameter binding logic in `Doctrine\ORM\Query\QueryExpressionVisitor`. The `walkComparison` method _does not_ properly handle parameter naming _such that_ parameters are assigned unique names to which the right values can be bound. Instead, the _same_ parameter names are being used/reused and overwritten with each condition on the same column. This is definitely fixed in the master branch (currently v2.5) but the problem exists in 2.4. The simplest solution is to add an incremental, numerical suffix to parameter names based on the number of parameters defined so that every single parameter is assured to be unique for value binding purposes. Ex (line 137): `$parameterName = str_replace('.', '_', $comparison->getField()) . count($this->parameters);` In the master branch (currently v2.5), there's some iteration on parameter names happening to resolve this issue (lines 150 to 155) but this seems needlessly inefficient and overkill: `foreach($this->parameters as $parameter) { if($parameter->getName() === $parameterName) { $parameterName .= '_' . count($this->parameters); break; } }` Reference file: https://github.com/doctrine/doctrine2/blob/2.5/lib/Doctrine/ORM/Query/QueryExpressionVisitor.php
Author
Owner

@lcobucci commented on GitHub (Nov 21, 2017):

@hbedrosian clarifying one thing: master is v2.6.x-dev and NOT v2.5, v2.5 is on 2.5 branch (which you referenced correctly). 2.4 is not supported anymore too.

but this seems needlessly inefficient and overkill

If you have any suggestion on how to improve it, please send a PR 😄

@lcobucci commented on GitHub (Nov 21, 2017): @hbedrosian clarifying one thing: `master` is `v2.6.x-dev` and NOT `v2.5`, `v2.5` is on `2.5` branch (which you referenced correctly). `2.4` is not supported anymore too. > but this seems needlessly inefficient and overkill If you have any suggestion on how to improve it, please send a PR 😄
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#3550