K
K
KerryDarko2016-02-05 12:51:02
Doctrine ORM
KerryDarko, 2016-02-05 12:51:02

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')
}

But the added parameters replace the old ones. Am I missing the documentation, or is there really no need for this and the parameters are really always added en masse? And if I want to break everything into methods?
Does it make sense to use:
$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

3 answer(s)
A
Alexey Romanenko, 2016-02-05
@KerryDarko

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'));
}

or
$params = [];
if ($getParams->get('name')) {
  $qb->andWhere('t.name = :name');
  $params['name'] =  $getParams->get('name');
}
$qb->setParameters($params);

what parameters replace the old ones? what do you mean?

A
Anton Natarov, 2016-02-05
@HanDroid


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.

K
KerryDarko, 2016-02-05
@KerryDarko

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 question

Ask a Question

731 491 924 answers to any question