我有这个 symfony 代码,它检索与我的项目中的博客部分相关的所有类别:
$category = $catrep->createQueryBuilder('cc')
->Where('cc.contenttype = :type')
->setParameter('type', 'blogarticle')
->getQuery();
$categories = $category->getResult();
这有效,但查询包含重复项:
Test Content
Business
Test Content
我想DISTINCT
在我的查询中使用该命令。我见过的唯一示例要求我编写原始 SQL。我想尽可能避免这种情况,因为我试图保持所有代码相同,因此它们都使用 Symfony2/Doctrine 提供的 QueryBuilder 功能。
我尝试distinct()
像这样添加到我的查询中:
$category = $catrep->createQueryBuilder('cc')
->Where('cc.contenttype = :type')
->setParameter('type', 'blogarticle')
->distinct('cc.categoryid')
->getQuery();
$categories = $category->getResult();
但这会导致以下错误:
致命错误:调用未定义的方法 Doctrine\ORM\QueryBuilder::distinct()
我如何告诉 symfony 选择不同的?