如何在createQueryBuilder中使用通配符?
在我的存储库类中,我使用:
public function getItemsByTag($tag)
{
$qb = $this->createQueryBuilder('c')
->select('c')
->where('c.tags LIKE %bipolar%')
->addOrderBy('c.id');
return $qb->getQuery()
->getResult();
}
但不幸的是,这不起作用..有人知道这是如何工作的吗?或者我是否必须在没有 QueryBuilder 的情况下构建自定义查询?
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我对 Symfony 不太了解,但根据我对 PHP 和 MySQL 的了解,我想你的意思是
'c.tags LIKE "%bipolar%"'
。您可能需要在%bipolar%
两边加上引号。I don't know much about Symfony, but based on what I know about PHP and MySQL, I imagine you mean
'c.tags LIKE "%bipolar%"'
. You likely need quotation marks around%bipolar%
.简单地:
simply:
基于单个参数进行搜索:
我认为应该这样:
但我认为不鼓励将 LIKE 作为使用查询生成器的 where 的一部分,因此您应该这样做:
查看文档更多信息,标题为 辅助方法
我还应该指出,我在每个示例中使用了不同的约定将参数传递到查询中,第一个使用命名参数
:tag
,其中由setParameter('tag', $value)
设置,第二个参数只是一个编号参数?1
,您可以轻松地在第二个参数中使用命名参数例如,如果您也愿意的话。使用一组参数进行搜索:
您还询问了如何进行一系列点赞。这里使用 OR 表达式,但如果您想搜索所有标签,可以将其更改为 AND。
为了创建“LIKE 数组”,您只需自行构建表达式。
Searching based on a single parameter:
I think it should go:
But I think that it is discouraged to do a LIKE as part of a where using the query builder so you should do:
Check out the docs for more information, there is an example of a like expression in the section entitled Helper Methods
I should also point out that I used a different convention in each example for passing a parameter into a query, the first used a named parameter
:tag
which is set bysetParameter('tag', $value)
the second is just a numbered parameter?1
, you could have just as easily have used a named parameter in the second example if you wished to as well.Searching with an array of parameters:
You also asked about doing an array of likes. Here it is with an OR expression but if you wanted to search for all tags you could change it to an AND.
In order to make a "LIKE array" you just have to build up the expression on its own.
如果您不想用变量替换查询而是使用静态字符串,则必须将该字符串放在撇号中。
您必须使用撇号而不是引号!否则 Doctrine2 Lexer 将抛出异常。
所以在你的情况迈克你可以使用:
或
If you don't want to substitute your query with variables but use a static string you have to put the string in apostrophes.
You have to use apostrophes instead of quotes! Otherwise the Doctrine2 Lexer will throw an Exception.
So in your case Mike you can use:
or