Search code examples
phpsymfonydql

Column comparison (upper)


I'm pretty new with Symfony and I would like to know how to proceed to compare a column with a string.

I'm using the QueryBuilder in a repository. The equivalent WHERE clause in MySQL is :

WHERE UPPER(a.name) = UPPER(my_app_var)
class ApplicationRepository extends EntityRepository
{
    public function existsByName($name, $caseSensitive = false)
    {
        $builder = $this->createQueryBuilder('a');

        if ($caseSensitive)
        {
            $builder->where($builder->expr()->eq('a.name', $name));
        }
        else
        { 
            $builder->where($builder->expr()->eq('a.name', $builder->expr()->upper($name)));
        }

        return count($builder->getQuery()->getResult()) != 0; 
    }
}

Symfony returns me a syntax error :

QueryException: SELECT a FROM Application a WHERE a.name = UPPER(my_var_app)


Solution

  • You need to encase your final variable in '' (single quotes) for the upper call. You will also need "" (double quotes) for variable interpolation

       $builder->where($builder->expr()
                       ->eq('a.name'
                            ,$builder->expr()
                             ->upper("'$name'")));   //// here