Answer the question
In order to leave comments, you need to log in
Doctrine: QueryBuilder: Is it possible to add parameters to those already set?
With the help of QueryBuilder, I can add any condition to the ones already added and not replace them:
if (isset($params['cityId'])) {
$qb->andWhere('c.city = :cityId');
}
if (isset($params['categoryId'])) {
$qb->andWhere('c.categoryId = :catId')
}
$parameters = $qb->getParameters();
$parameters[] = new Parameter('cityId', $params['cityId']);
$qb->setParameters($parameters);
Answer the question
In order to leave comments, you need to log in
Nothing is clear from the question at all, but judging by the clarifications, you do not know about the setParameter () method.
If you need to add something according to the condition. I do this (pseudocode):
if ($getParams->get('name')) {
$qb->andWhere('t.name = :name')
->setParameter('name', $getParams->get('name'));
}
$params = [];
if ($getParams->get('name')) {
$qb->andWhere('t.name = :name');
$params['name'] = $getParams->get('name');
}
$qb->setParameters($params);
you write ->getQuery
at the end of your request ,
only after this command a request is formed and goes to the database.
Up to this point, you can compose the request however you like.
The problem was elsewhere.
Yes, parameters can be added one at a time via setParameter()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question