php 如何使用symfony2学说查询构建器选择不同的查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7188219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:13:00  来源:igfitidea点击:

How to select distinct query using symfony2 doctrine query builder?

phpdoctrine-ormsymfonyquery-builder

提问by mickburkejnr

I have this symfony code where it retrieves all the categories related to a blog section on my project:

我有这个 symfony 代码,它检索与我的项目中的博客部分相关的所有类别:

$category = $catrep->createQueryBuilder('cc')
    ->Where('cc.contenttype = :type')
    ->setParameter('type', 'blogarticle')
    ->getQuery();

$categories = $category->getResult();

This works, but the query includes duplicates:

这有效,但查询包含重复项:

Test Content
Business
Test Content

I want to use the DISTINCTcommand in my query. The only examples I have seen require me to write raw SQL. I want to avoid this as much as possible as I am trying to keep all of my code the same so they all use the QueryBuilder feature supplied by Symfony2/Doctrine.

我想DISTINCT在我的查询中使用该命令。我见过的唯一示例要求我编写原始 SQL。我想尽可能避免这种情况,因为我试图保持所有代码相同,以便它们都使用 Symfony2/Doctrine 提供的 QueryBuilder 功能。

I tried adding distinct()to my query like this:

我尝试distinct()像这样添加到我的查询中:

$category = $catrep->createQueryBuilder('cc')
    ->Where('cc.contenttype = :type')
    ->setParameter('type', 'blogarticle')
    ->distinct('cc.categoryid')
    ->getQuery();

$categories = $category->getResult();

But it results in the following error:

但它导致以下错误:

Fatal error: Call to undefined method Doctrine\ORM\QueryBuilder::distinct()

致命错误:调用未定义的方法 Doctrine\ORM\QueryBuilder::distinct()

How do I tell symfony to select distinct?

我如何告诉 symfony 选择不同的?

采纳答案by Raffael

you could write

你可以写

select DISTINCT f from t;

as

作为

select f from t group by f;

thing is, I am just currently myself getting into Doctrine, so I cannot give you a real answer. but you could as shown above, simulate a distinct with group byand transform that into Doctrine. if you want add further filtering then use HAVINGafter group by.

问题是,我目前刚刚进入 Doctrine,所以我不能给你一个真正的答案。但是您可以如上所示,使用group by模拟一个不同的对象并将其转换为Doctrine。如果你想添加进一步的过滤,那么HAVING在 group by之后使用。

回答by skler

This works:

这有效:

$category = $catrep->createQueryBuilder('cc')
        ->select('cc.categoryid')
        ->where('cc.contenttype = :type')
        ->setParameter('type', 'blogarticle')
        ->distinct()
        ->getQuery();

$categories = $category->getResult();

回答by Chris

If you use the "select()" statement, you can do this:

如果您使用“select()”语句,您可以这样做:

$category = $catrep->createQueryBuilder('cc')
    ->select('DISTINCT cc.contenttype')
    ->Where('cc.contenttype = :type')
    ->setParameter('type', 'blogarticle')
    ->getQuery();

$categories = $category->getResult();

回答by Ali

Just open your repository file and add this new function, then call it inside your controller:

只需打开您的存储库文件并添加这个新函数,然后在您的控制器中调用它:

 public function distinctCategories(){
        return $this->createQueryBuilder('cc')
        ->where('cc.contenttype = :type')
        ->setParameter('type', 'blogarticle')
        ->groupBy('cc.blogarticle')
        ->getQuery()
        ->getResult()
        ;
    }

Then within your controller:

然后在您的控制器中:

public function index(YourRepository $repo)
{
    $distinctCategories = $repo->distinctCategories();


    return $this->render('your_twig_file.html.twig', [
        'distinctCategories' => $distinctCategories
    ]);
}

Good luck!

祝你好运!