按DQL

时间:2015-10-31 11:02:22

标签: php symfony query-builder doctrine-query

您好我已经使用查询构建器对排名机制进行了简单查询。

        $result = $qb
            ->select('u')
            ->where('u.status = 1')
            ->from('PGMainBundle:User', 'u')
            ->groupBy('u.id')
            ->addSelect('COUNT(c.id) as HIDDEN nChallenges')
            ->leftJoin('u.challenges', 'c', 'WITH', 'c.closed = 1' )
            ->add('orderBy','u.points DESC, nChallenges DESC')
            ->orderBy('u.points', 'DESC')
            ->addOrderBy('nChallenges', 'DESC')
            ->setFirstResult($offset*50)
            ->setMaxResults(50)
            ->getQuery()
            ->getResult();

现在我的排名机制工作正常,我想查看具有$ id的用户的loop.index。

说这个,我不想在结果上使用foreach循环来这样做。

是否有更优化的方式只是为了返回"位置"在排名?

可能使用查询构建器吗?

1 个答案:

答案 0 :(得分:1)

结果应该是array collection,这样你就可以获得给定元素的索引:

$result->indexOf($yourelement)

否则,如果密钥不是有序的,但是是实体的id:

  $keys = $result->getKeys();
  $id = $yourElement->getId();
  $position = array_search($id, $keys);