如何在createQueryBuilder中使用通配符? [英] How to use wildcards in createQueryBuilder?

查看:144
本文介绍了如何在createQueryBuilder中使用通配符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的存储库类中,我使用:

In my repository class i use:

public function getItemsByTag($tag)
{
    $qb = $this->createQueryBuilder('c')
               ->select('c')
               ->where('c.tags LIKE %bipolar%')
               ->addOrderBy('c.id');

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

但是不幸的是,这行不通。有人知道这怎么行吗?还是我必须在没有QueryBuilder的情况下构建自定义查询?

But unfortunately this doesn't work.. Anybody knows how this can work? Or do I have to build a custom query without the QueryBuilder?

谢谢!

推荐答案

如果您不想用变量替换查询,而是使用静态字符串,则必须将字符串放在撇号中。

If you don't want to substitute your query with variables but use a static string you have to put the string in apostrophes.

必须使用撇号代替引号!否则,Doctrine2 Lexer将引发异常。

You have to use apostrophes instead of quotes! Otherwise the Doctrine2 Lexer will throw an Exception.

因此,在您的情况下,Mike您可以使用:

So in your case Mike you can use:

'c.tags LIKE \'%bipolar%\''

"c.tags like '%bipolar%'"

这篇关于如何在createQueryBuilder中使用通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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