【问题标题】:How to select distinct query using symfony2 doctrine query builder?如何使用 symfony2 学说查询生成器选择不同的查询?
【发布时间】:2011-11-03 12:45:25
【问题描述】:

我有这个 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 选择 distinct?

【问题讨论】:

标签: php doctrine-orm symfony query-builder


【解决方案1】:

这行得通:

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

$categories = $category->getResult();

为 Symfony 3 和 4 编辑。

您应该使用->groupBy('cc.categoryid') 而不是->distinct()

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      你可以写

      select DISTINCT f from t;
      

      作为

      select f from t group by f;
      

      问题是,我自己现在才刚刚进入 Doctrine,所以我无法给你一个真正的答案。但是您可以如上所示,使用group by 模拟一个不同的并将其转换为Doctrine。如果您想添加进一步过滤,请在分组后使用HAVING

      【讨论】:

      • @mickburkejnr 直到您添加 ORDER 子句。 :(
      • @xyu 我知道,真的不理想是吗?我们为什么要被这样的事情所困扰?为什么它不能正常工作?
      • @mickburkejnr 分组发生在 SQL 中排序之前。
      • 如果您使用的是 sql 语句而不是查询生成器,这只是正确的答案。这不应被标记为@skler 给出的解决方案是正确的答案。
      • (旧评论,我知道...)。 @Tom T:实际上,这个答案是有效的。意思是不要尝试使用 actual DISTINCT 关键字,而是在 QueryBuilder 中使用 GROUP BY 模拟它。然而,他没有举出一个例子。这是一个简单的:$q = $db->createQueryBuilder(); $q->select('f')->from('t', 't')->groupBy('f');
      【解决方案4】:

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

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

      然后在你的控制器中:

      public function index(YourRepository $repo)
      {
          $distinctCategories = $repo->distinctCategories();
      
      
          return $this->render('your_twig_file.html.twig', [
              'distinctCategories' => $distinctCategories
          ]);
      }
      

      祝你好运!

      【讨论】:

      • “没有工作”是什么意思?相反会发生什么?为什么这应该是 Symfony 的问题,而查询构建器是 Doctrine 的一部分?
      • @NicoHaase,因为通过 composer 创建一个新的 Symfony 项目将安装最新版本的 Twig 和 Doctrine,这就是为什么我提到我的解决方案是用 Symfony5 和任何 Doctrine 版本一起测试的。希望现在更清楚了。
      猜你喜欢
      • 2016-04-02
      • 2013-09-29
      • 2015-07-03
      • 2012-12-07
      • 1970-01-01
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      相关资源
      最近更新 更多