Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine Query Builder LOWER not working

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?

like image 954
apfz Avatar asked Jul 07 '13 11:07

apfz


1 Answers

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.

like image 73
apfz Avatar answered Oct 31 '22 10:10

apfz