title: Get users by role
body: <p>I have an array of entities (groups) in an entity (user), and in my Query Builder I would like to retrieve users by role.</p>
  <p>I'm trying with this:</p>
  <pre><code>public function findUsersByRole($role){
      $qb = $this-&gt;createQueryBuilder('u');
      $qb-&gt;select('u')
         -&gt;where('u.roles LIKE :role')
         -&gt;setParameter('role', $role);
  
      return $qb-&gt;getQuery()-&gt;getResult();
  }
  </code></pre>
  <p>But I don't get results.</p>
  <p>I tried also:</p>
  <pre><code>public function findUsersByRole($role)
  {
    $qb = $this-&gt;createQueryBuilder('u');
    $qb-&gt;select('u')
        -&gt;where(':role MEMBER OF u.roles')
        -&gt;setParameter('role', $role);
    return $qb-&gt;getQuery()-&gt;getResult();
  }
  </code></pre>
  <p>But I'm getting the error:</p>
  <blockquote>
  <p>[Semantical Error] line 0, col 62 near 'roles': Error: Invalid
  PathExpression. Must be a CollectionValuedAssociationField.</p>
  </blockquote>
  <p>What am I doing wrong?</p>
  <p>The roles are stored in the user table field roles field that is of JSON type, like this:</p>
  <pre><code>[
      &quot;ROLE_USER&quot;,
      &quot;ROLE_ADMIN&quot;
  ]
  </code></pre>
  <p>This is the function of the <strong>User class</strong> that stores the user role in the database:</p>
  <pre><code>public function setRoles(array $roles){
      $this-&gt;roles = $roles;
  }
  </code></pre>
  <p>This is the unit test I'm writing to test the <strong>testFindUsersByRole</strong> function:</p>
  <pre><code>public function testFindUsersByRole() {
      $user1 = $this-&gt;createUser(1, &quot;Developer&quot;, &quot;John&quot;, &quot;Smith&quot;, &quot;smith@test.com&quot;);
      $user2 = $this-&gt;createUser(2, &quot;Developer&quot;, &quot;Joe&quot;, &quot;Black&quot;, &quot;joe@test.com&quot;);
      $user3 = $this-&gt;createUser(3, &quot;Developer&quot;, &quot;Jack&quot;, &quot;Blade&quot;, &quot;blade@test.com&quot;);
  
      $roles1 = ['ROLE_ADMIN','ROLE_USER'];
      $roles2 = ['ROLE_USER'];
  
      $user1-&gt;setRoles($roles1);
      $user2-&gt;setRoles($roles2);
  
      $response = $this-&gt;userRepository-&gt;findUsersByRole('ROLE_ADMIN');
      $this-&gt;assertCount(1, $response);
  
      $this-&gt;purge([$user1, $user2, $user3]);
  }
  </code></pre>