Question

I'm trying to retrieve the most active Users ordered by the number of their comments.

This DQL query returns one object ...

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

How can i resolve this issue?

EDIT 2:

Ok, I think I'm not far. With this:

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

I retrieve the two User Object, but I on't understand how access to the COUNT() result for each...

EDIT:

I took a look on NativeQuery, and it's seem to be possible, but I don't understand why this is not working:

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

This return only one object...

But I want one Object for each User, with, it's possible, an 'abstract attribute' (I just invent this world, don't Google it...) nbQuestions for each of them.

i don't know if I'm really clear...

Thanks

Was it helpful?

Solution

After read Dotrine Doc again, I've found a solution. Like my second Edit, you just have to create a ScalarField. I give you an exemple.

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

In this exemple, Doctrine return an array of User, and you can access to them by :

(we suppose that $askers have the return result of the code below)

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

And if you want your ScalarField, Doctrine set a new field in the return array, so:

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

Thank, Hope it's could help!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top