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 DISTINCT
command 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.
我想在查询中使用不同的命令。我看到的唯一示例要求我编写原始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()
致命错误:调用未定义的方法原则\ORM\QueryBuilder::distinct()
How do I tell symfony to select distinct?
如何告诉symfony选择不同的?
3 个解决方案
#1
24
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 by and transform that into Doctrine. if you want add further filtering then use HAVING
after group by.
问题是,我现在自己也在学习教义,所以我不能给你一个真实的答案。但是正如上面所示,可以模拟一个与group by不同的组,并将其转换为Doctrine。如果你想添加进一步的过滤,那么使用after group by。
#2
140
This works:
如此:
$category = $catrep->createQueryBuilder('cc')
->select('cc.categoryid')
->where('cc.contenttype = :type')
->setParameter('type', 'blogarticle')
->distinct()
->getQuery();
$categories = $category->getResult();
#3
43
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();
#1
24
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 by and transform that into Doctrine. if you want add further filtering then use HAVING
after group by.
问题是,我现在自己也在学习教义,所以我不能给你一个真实的答案。但是正如上面所示,可以模拟一个与group by不同的组,并将其转换为Doctrine。如果你想添加进一步的过滤,那么使用after group by。
#2
140
This works:
如此:
$category = $catrep->createQueryBuilder('cc')
->select('cc.categoryid')
->where('cc.contenttype = :type')
->setParameter('type', 'blogarticle')
->distinct()
->getQuery();
$categories = $category->getResult();
#3
43
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();