Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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%');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading