7

Somehow doctrine is not allowing me to compare two lowerstring values: that of a variable and the first name of a user.

    $qb = $this->getEntityManager()->createQueryBuilder();
    $qb
        ->select('d')
        ->from('MyBundle:User', 'd')
        ->where('LOWER(d.firstName) LIKE :fName')
        ->setParameter('fName', strtolower('%'.$fName.'%'));

    $result = $qb->getQuery()->execute();

only when $fName has an uppercase string (i.e. 'Rob'), it will return results like 'Robert' and 'Robby'. But what I want is that even when $fName is spelled lowercase ('rob'), these results should come up. It seems that the d.firstNames are not lowered. Why is this the case?

xfscrypt
  • 16
  • 5
  • 28
  • 59

2 Answers2

9

I found the solution:

        $qb = $this->getEntityManager()->createQueryBuilder();
        $qb
            ->select('u')
            ->from('MyBundle:User', 'u')
            ->where($qb->expr()->andX(
                $qb->expr()->like('LOWER(u.firstName)', '?1'),
                $qb->expr()->like('LOWER(u.lastName)', '?2')
            ))
            ->setParameter('1', '%' . strtolower($firstName) . '%')
            ->setParameter('2', '%' . strtolower($lastName) . '%');

        $result = $qb->getQuery()->execute();

Still curious why the first attempt was not working.

xfscrypt
  • 16
  • 5
  • 28
  • 59
-2

you can use :

mb_strtolower()

http://php.net/manual/en/function.mb-strtolower.php

EmmCall
  • 168
  • 1
  • 1
  • 13