I am making a symfony application with doctrine. I'm using a queryBuilder to fetch some information from the database, however, I cannot bind parameters to my query.
I'm using this repository method to fetch my data from the database:
public function getRequestPaginator($offset = 0, RequestFilter $filter = null)
{
if ($filter == null) {
$filter = new RequestFilter();
}
$query = $this->createQueryBuilder('r');
foreach ($filter->getStates() as $key => $value) {
//preferably I would like to use parameters here too
$query = $query->orWhere("r.state = '$value'");
}
$query = $query
//this part breaks
->orderBy(':order')
->setParameter('order', $filter->getOrder())
//->orderBy('r.' . $filter->getOrder(), $filter->getOrderDirection())
->setFirstResult($offset)
->setMaxResults($filter->getRequestsPerPage())
->getQuery();
return new Paginator($query);
Once this code is run in my controller, I get this error at the "orderBy" liner:
Error: Expected Doctrine\ORM\Query\Lexer::T_IDENTIFIER, got ':order'
This is the same with numbered parameters ( ?1 instead of :order) and I just can't understand why doctrine is rejecting this when I'm just following the official documentation.
Here's my doctrine version:
"doctrine/annotations": "^1.0",
"doctrine/doctrine-bundle": "^2.6",
"doctrine/doctrine-migrations-bundle": "^3.2",
"doctrine/orm": "^2.11",
I am running PHP 8.1 and symfony 6.0.7 on WAMP.
Thank you in advance for your advice. If this question needs more information, I will provide it quickly !
as @craigh said, I was using parameters inside orderBy(), which does not seem to be supported. Using
->orderBy($filter->getOrder())
works.