列比较(上)

时间:2013-03-20 15:25:48

标签: php symfony dql

我对Symfony很新,我想知道如何继续比较一个字符串。

我在存储库中使用QueryBuilder。 MySQL中等效的WHERE子句是:

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返回一个语法错误:

  

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

1 个答案:

答案 0 :(得分:1)

您需要将最终变量包含在''(单引号)中以进行上限调用。您还需要“”(双引号)进行变量插值

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