Search code examples
phpforeachdoctrine-ormquery-builderdql

doctrine dql/queryBuilder with a foreach or array in parameters


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.


Solution

  • 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