5

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();
    }
3
  • How are you storing the ur.role ? Is this a separate entity or it's a column inside the User table? Commented Apr 18, 2016 at 16:08
  • Its a colum inside the user table Commented Apr 18, 2016 at 16:14
  • I think you should probably $roleUser = implode(',', $this->container->getParameter('user_role')); instead of actually passing the array directly. Commented Apr 18, 2016 at 16:23

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.