按 DQL 排序的对象的 Symfony2 索引

Symfony2 index of an object sorted by DQL

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

        $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();

现在虽然我的排名机制工作正常,但我想检查 loop.index 具有 $id 的用户拥有什么。

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

有没有更优化的方法只为了return排名"position"?

可能使用查询构建器?

结果应该是 array collection 这样您就可以像这样获取给定元素的索引 :

$result->indexOf($yourelement)

否则,如果键没有按顺序排列,而是实体的 ID:

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