Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2.5 : sql query with Where IN Clause using an array as a parameter

I am trying to run sql query. I am trying to select users with specific roles. Below my query:

  $roleUser= $this->container->getParameter('user_role');

  $query = $this->getDoctrine()->getEntityManager()
      ->createQuery(
          'SELECT u FROM UserBundle:User ur
          WHERE ur.role  IN :role_user')
     ->setParameters('role_user',$roleUser);

In my config.yml I defined my user roles

parameters:
  user_role:
   - ADMIN1
   - ADMIN2
   - ADMIN3

I want to select all the user that has one of the role ADMIN1, ADMIN2 or ADMIN3.

Here is the error message:

Warning: Invalid argument supplied for foreach() in /Test/vendor/doctrine/orm/lib/Doctrine/ORM/Query.php line 246
{
    $types = array();
    foreach ($this->parameters as $parameter) {
        /** @var Query\Parameter $parameter */
        $types[$parameter->getName()] = $parameter->getType();
    }
like image 250
Th Th Avatar asked Oct 31 '22 05:10

Th Th


1 Answers

Try with setParameter instead of setParameters:

try this:

 ->setParameter('role_user',$roleUser);

instead of this:

 ->setParameters('role_user',$roleUser);

More info about setting parameters here in the doc.

Hope this help

like image 126
Matteo Avatar answered Nov 15 '22 07:11

Matteo