Search code examples
mysqldoctrine-ormdql

DQL returns 0 expected result where MySQL has 2


I have a doctrine query (DQL) on my user repository which returns 0 results when I use a like :

$qb = $this->createQueryBuilder('q');
$qb->select('q.id, q.roles');
$qb->andWhere($qb->expr()->like('q.roles', ':role1'));
$qb->setParameter('role1', '"%ROLE_ADMIN%"');
return $qb->getQuery()->getResult();

whereas if I manually execute the query on MySQL, I have 2 results

SELECT * FROM user u WHERE u.roles LIKE "%ROLE_ADMIN%"

I have 2 users have "ROLE_ADMIN"

I do not understand

I cleared the cache

I tried to make a query that returns all the users, and it works, the 2 ROLE_ADMIN are in it


Solution

  • You should not use the double qoutes inside ::setParameter() call, as they would be interpreted as a part of searched expression. Use only one pair of quotes.

    $qb->setParameter('role1', '%ROLE_ADMIN%');