1
votes

In my symfony aplication, I recover a variable as a string by my ajax script.

This variable looks like this:

$slug = "1-2-3-4-4-5-6-7" /* it could more*/
$vars = explode('-',$slug ); /*push the $slug string in an array */

/* recover all value of the array */
foreach ($vars as $var) {
      echo $var;
    }

Now I need to use this foreach in a dql or a query builder because all $var are ids of an entity and i need to pass all this value to get the right result.

How can I make a query with an array of parameter or make a foreach in doctrine query builder ?

I have already try something this in a queryBuilder method:

->setParameters("id" => $vars);

it returns me an error : SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 1 column(s)

I try this too in the queryBuilder:

->setParameters(array('id' => array($vars)));

Here I have no error, but it does not return the good result. Indeed I have only one result in database if there are many ids.

3

3 Answers

1
votes

If you want to check if the id is within a certain set of given ids you can use IN.

$qb = $this->createQueryBuilder('s')
        ->addSelect('u')
        ->innerJoin('s.field','u')
        ->where("u.id IN(:ids)")
        ->setParameter('ids', array_values($ids));
0
votes

Try using the ->setParameter('ids', $ids). where $ids is the array of ids

0
votes

The solution is here.

I created a more complex query in order to answer to all condition:

array is empty

array contains only 1 value

array contains many values