Doctrine - 如何获取按相关实体数量排序的实体(一对多)?

时间:2014-05-14 15:08:23

标签: symfony doctrine-orm dql

我正在尝试检索按其评论数量排序的最活跃用户。

此DQL查询返回一个对象...

    $query = $this->_em->createQuery(
        'select u, COUNT(q) qc
         FROM Btp\UserBundle\Entity\User u 
         JOIN u.questions q 
         ORDER BY qc'
        )
        ->setMaxResults($limit)
        ->getResult();

... while this query returns two objects as expected.

    $query = $this->_em->createQuery(
        'select u
         FROM Btp\UserBundle\Entity\User u 
         JOIN u.questions q'
        )
        ->setMaxResults($limit)
        ->getResult();

我该如何解决这个问题?

编辑2:

好的,我想我不远了。有了这个:

$rsm = new ResultSetMapping;
$rsm->addEntityResult('Btp\UserBundle\Entity\User', 'u');
$rsm->addFieldResult('u', 'id', 'id');
$rsm->addFieldResult('u', 'name', 'name');
$rsm->addJoinedEntityResult('Btp\UserBundle\Entity\Question' , 'q', 'u', 'questions');
$rsm->addFieldResult('q', 'question_id', 'id');
$rsm->addScalarResult('nb_questions', 'nbQuestions');

$sql = 'SELECT u.id, u.name, COUNT(q.id) as nb_questions
        FROM user AS u
        INNER JOIN question AS q ON u.id = q.auteur_id 
        GROUP BY u.id
        ORDER BY nb_questions ASC';
$query = $this->_em->createNativeQuery($sql, $rsm);
return ($query->getResult());

我检索了两个用户对象,但我不明白如何访问每个用户的COUNT()结果...

编辑:

我看了一下NativeQuery,它似乎有可能,但我不明白为什么这不起作用:

   $rsm = new ResultSetMapping;
            $rsm->addEntityResult('User', 'u');
            $rsm->addFieldResult('u', 'id', 'id');
            $rsm->addFieldResult('u', 'name', 'name');
            $rsm->addJoinedEntityResult('Question' , 'q', 'u', 'questions');
            $rsm->addFieldResult('q', 'question_id', 'id');
            $rsm->addScalarResult('nb_questions', 'nbQuestions');

            $sql = 'SELECT u.id, u.name, COUNT(q.id) as nb_questions
                    FROM user AS u
                    INNER JOIN question AS q ON u.id = q.auteur_id 
                    GROUP BY u.id
                    ORDER BY nb_questions ASC';
            $query = $this->_em->createNativeQuery($sql, $rsm);

这只返回一个对象......

但我想为每个用户提供一个对象,有可能是一个“抽象属性”(我只是发明了这个世界,不是谷歌......)每个用户的nbQuestions。

我不知道我是否真的很清楚...

谢谢

1 个答案:

答案 0 :(得分:0)

再次阅读Dotrine Doc后,我找到了解决方案。就像我的第二个Edit一样,你只需要创建一个ScalarField。我给你一个例子。

$rsm = new ResultSetMapping;
$rsm->addEntityResult('Btp\UserBundle\Entity\User', 'u');
$rsm->addFieldResult('u', 'id', 'id');
$rsm->addFieldResult('u', 'name', 'name');
$rsm->addJoinedEntityResult('Btp\UserBundle\Entity\Question' , 'q', 'u', 'questions');
$rsm->addFieldResult('q', 'question_id', 'id');
$rsm->addScalarResult('nb_questions', 'nbQuestions');

$sql = 'SELECT u.id, u.name, COUNT(q.id) as nb_questions
        FROM user AS u
        INNER JOIN question AS q ON u.id = q.auteur_id 
        GROUP BY u.id
        ORDER BY nb_questions ASC';
$query = $this->_em->createNativeQuery($sql, $rsm);
return ($query->getResult());

在这个例子中,Doctrine返回一个User数组,您可以通过以下方式访问它们:

(我们假设$ askers有以下代码的返回结果)

$askers->[0][0]->getName() //return the name of the first User in array

如果你想要你的ScalarField,Doctrine会在返回数组中设置一个新字段,所以:

$askers[0]["nbQuestions"] // give the result of your ScalarField, in this case the question's number of the first User of the array

谢谢,希望它可以提供帮助!