我正在尝试对doctrine 2查询中的实体执行多次计数。
当我执行单次计数时,我能够正确获取计数,但是当我在同一查询中执行多次计数时会遇到问题。
此外,我的查询返回实体;但我不想要这个实体。我只想要点数。
这是我的查询;
public function getMessagesCount()
{
$qb = $this->queryBuilder();
$qb->select(array('u','count(fa)as favouritesAdded','count(se)as sentMessages' ))
->from('BaseModel\Entity\User','u')
->leftJoin('u.theFavouriteAdded', 'fa')
->leftJoin('u.sentMessagesObject', 'se')
->where('u.id = :identifier')
->setParameter('identifier',(int)$this->getUserId());
$query = $qb->getQuery();
return $data = $query->getArrayResult();
}
此查询返回两个计数的相同数字(即错误地返回每个计数20)
然而,当我每次只做一次计数时;他们每次查询都返回了正确的帐户。即;
public function getMessagesCounts()
{
$qb = $this->queryBuilder();
$qb->select(array('u','count(fa)as favouritesAdded' ))
->from('BaseModel\Entity\User','u')
->leftJoin('u.theFavouriteAdded', 'fa')
// ->leftJoin('u.sentMessagesObject', 'se')
->where('u.id = :identifier')
->setParameter('identifier',(int)$this->getUserId());
$query = $qb->getQuery();
return $data = $query->getArrayResult();
}
我尝试在查询中添加一个组;但它仍然返回了错误的数字.i.e
->groupBy("u.favouritesAdded","u.sentMessages")
对查询进行多次计数的正确方法是什么?
答案 0 :(得分:0)
(返回计数而不返回实体)
POST
public function getMessagesCounts()
{
$qb = $this->queryBuilder();
$qb->select('count(se)')
->from('BaseModel\Entity\User','u')
->leftJoin('u.sentMessagesObject', 'se')
->where('u.id = :identifier')
->setParameter('identifier', (int) $this->getUserId());
return $qb->$query->getResult();
}