Symfony 查询 =>错误:预期文字,得到“" [英] Symfony query => Error: Expected Literal, got '"'

查看:85
本文介绍了Symfony 查询 =>错误:预期文字,得到“"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询:(它在没有 addSelect('CASE ... END AS HIDDEN loquer') 部分的情况下工作正常目标是在我的评论中添加一些顺序.但是对于这部分,来自 symfony 的以下消息被触发:错误:预期文字,得到 '"'

I have the following query: (it works fine without the addSelect('CASE ... END AS HIDDEN loquer') part which goals is to add some order into my comments. but with this part the following message from symfony is triggered: Error: Expected Literal, got '"'

public function myfindArticleandCommentsandScores($article,$language){
    $qb = $this->createQueryBuilder('a');
    $qb->leftjoin('a.comments','c')
        ->addSelect('c')
        ->leftJoin('c.scores','s')
        ->addSelect('s')
        ->leftJoin('s.user','u')
        ->addSelect('u')
        ->addSelect('
                CASE 
                    WHEN c.show = "yes" THEN 1  // PROBLEME IS HERE
                    ELSE 2
                END AS HIDDEN show_order  
            ')
        ->where(
                    $qb->expr()->eq('a.id', '?1'),
                    $qb->expr()->orX(
                        $qb->expr()->eq('c.langue', '?2'),
                        $qb->expr()->eq('c.langue', '?3')
                        )
                )
        ->setParameters(array(
                    '1'=> $article,
                    '2'=> $language,
                    '3'=> 'EN',
                ))
        ->orderBy('show_order', 'ASC')
        ->addOrderBy('c.scorenote', 'DESC');

    return $qb->getQuery()
                ->getResult();
}

我尝试将yes"替换为yes",但收到以下消息:FatalErrorException:解析:语法错误,意外的oui"(T_STRING)

I tried to replace "yes" with 'yes' but then I got the following message: FatalErrorException: Parse: syntax error, unexpected 'oui' (T_STRING)

推荐答案

使用 setParameter 将占位符替换为字符串或使用"xyz='yes'"

Use setParameter to replace a placeholder with a string or use "xyz='yes'"

现在代码应该是什么样子(一种可能的方式):

Now what the code should looks like (one possible way):

public function myfindArticleandCommentsandScores($article,$language){
    $qb = $this->createQueryBuilder('a');
    $qb->leftjoin('a.comments','c')
        ->addSelect('c')
        ->leftJoin('c.scores','s')
        ->addSelect('s')
        ->leftJoin('s.user','u')
        ->addSelect('u')
        ->addSelect('
                CASE 
                    WHEN c.show = \'yes\' THEN 1  // PROBLEME IS HERE
                    ELSE 2
                END AS HIDDEN show_order  
            ')
        ->where(
                    $qb->expr()->eq('a.id', '?1'),
                    $qb->expr()->orX(
                        $qb->expr()->eq('c.langue', '?2'),
                        $qb->expr()->eq('c.langue', '?3')
                        )
                )
        ->setParameters(array(
                    '1'=> $article,
                    '2'=> $language,
                    '3'=> 'EN',
                ))
        ->orderBy('show_order', 'ASC')
        ->addOrderBy('c.scorenote', 'DESC');

    return $qb->getQuery()
                ->getResult();
}

这篇关于Symfony 查询 =>错误:预期文字,得到“"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆