如何在createQueryBuilder中使用通配符?

发布于 2024-12-18 10:25:31 字数 399 浏览 4 评论 0 原文

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

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

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

但不幸的是,这不起作用..有人知道这是如何工作的吗?或者我是否必须在没有 QueryBuilder 的情况下构建自定义查询?

谢谢!

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();
}

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

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷心人i 2024-12-25 10:25:32

我对 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%.

梦言归人 2024-12-25 10:25:32

简单地:

public function getItemsByTag($tag)
{
    $qb = $this->createQueryBuilder('c')
           ->select('c')
           ->where( $qb->expr()->like('c.tags', ':tags') )
           ->addOrderBy('c.id');

    $qb->setParameter('tags', '%' . $tag . '%' );

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

simply:

public function getItemsByTag($tag)
{
    $qb = $this->createQueryBuilder('c')
           ->select('c')
           ->where( $qb->expr()->like('c.tags', ':tags') )
           ->addOrderBy('c.id');

    $qb->setParameter('tags', '%' . $tag . '%' );

    return $qb->getQuery()->getResult();
}
遮云壑 2024-12-25 10:25:31

基于单个参数进行搜索:

我认为应该这样:

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

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

但我认为不鼓励将 LIKE 作为使用查询生成器的 where 的一部分,因此您应该这样做:

$qb = $this->createQueryBuilder('c');

$qb->select('c')
   ->where($qb->expr()->like('c.tags', '?1'))
   ->addOrderBy('c.id')
   ->setParameter(1, $tag);

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

查看文档更多信息,标题为 辅助方法

我还应该指出,我在每个示例中使用了不同的约定将参数传递到查询中,第一个使用命名参数 :tag ,其中由 setParameter('tag', $value) 设置,第二个参数只是一个编号参数 ?1,您可以轻松地在第二个参数中使用命名参数例如,如果您也愿意的话。

使用一组参数进行搜索

您还询问了如何进行一系列点赞。这里使用 OR 表达式,但如果您想搜索所有标签,可以将其更改为 AND。

为了创建“LIKE 数组”,您只需自行构建表达式。

$qb = $this->createQueryBuilder('c');

$orExpr = $qb->expr()->orX();

for ($i = 0; $i < count($tags); $i++) {
    $orExpr->add($qb->expr->like('c.tags', "?$i"));

    // You may have to set params later in a loop after $orExpr has been
    // added to the queryBuilder.
    $qb->setParameter($i, $tags[$i]);
}

$qb->select('c')->where($orExpr)->addOrderBy('c.id');

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

Searching based on a single parameter:

I think it should go:

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

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

But I think that it is discouraged to do a LIKE as part of a where using the query builder so you should do:

$qb = $this->createQueryBuilder('c');

$qb->select('c')
   ->where($qb->expr()->like('c.tags', '?1'))
   ->addOrderBy('c.id')
   ->setParameter(1, $tag);

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

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 by setParameter('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.

$qb = $this->createQueryBuilder('c');

$orExpr = $qb->expr()->orX();

for ($i = 0; $i < count($tags); $i++) {
    $orExpr->add($qb->expr->like('c.tags', "?$i"));

    // You may have to set params later in a loop after $orExpr has been
    // added to the queryBuilder.
    $qb->setParameter($i, $tags[$i]);
}

$qb->select('c')->where($orExpr)->addOrderBy('c.id');

return $qb->getQuery()->getResult();
2024-12-25 10:25:31

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

必须使用撇号而不是引号!否则 Doctrine2 Lexer 将抛出异常。

所以在你的情况迈克你可以使用:

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

"c.tags like '%bipolar%'"

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:

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

or

"c.tags like '%bipolar%'"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文