本文介绍了如何在Doctrine2中执行MySQL count(*)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Doctrine2查询:

I have the following Doctrine2 query:

$qb = $em->createQueryBuilder()
      ->select('t.tag_text, COUNT(*) as num_tags')
      ->from('CompanyWebsiteBundle:Tag2Post', 't2p')
      ->innerJoin('t2p.tags', 't')
      ->groupBy('t.tag_text')
;
$tags = $qb->getQuery()->getResult();

运行时出现以下错误:

[Semantical Error] line 0, col 21 near '*) as num_tags': Error: '*' is not defined.

如何在Doctrine2中执行MySQL count(*)?

How would I do MySQL count(*) in Doctrine2?

推荐答案

你试图在DQL中做不要在Doctrine 2中。

You're trying to do it in DQL not "in Doctrine 2".

指定要计算哪个字段(请注意,我不使用术语列),这是因为您正在使用ORM,并且需要以OOP方式思考。

You need to specify which field (note, I don't use the term column) you want to count, this is because you are using an ORM, and need to think in OOP way.

$qb = $em->createQueryBuilder()
      ->select('t.tag_text, COUNT(t.tag_text) as num_tags')
      ->from('CompanyWebsiteBundle:Tag2Post', 't2p')
      ->innerJoin('t2p.tags', 't')
      ->groupBy('t.tag_text')
;
$tags = $qb->getQuery()->getResult();

但是,如果您需要性能,可能需要使用,因为你的结果是简单的标量不是一个对象。

However, if you require performance, you may want to use a NativeQuery since your result is a simple scalar not an object.

这篇关于如何在Doctrine2中执行MySQL count(*)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 04:49