0

I'm using Doctrine and DQL function createQuery() and I have this query :

SELECT id, 1 + (
    SELECT count( * )
    FROM user a
    WHERE a.points > b.points ) AS RNK, points
FROM user b

This query return a rank calculated with the field points.

How can I convert this query for use in createQuery() ?

For now I have :

$query = $this->getEntityManager()->createQuery("
    SELECT id, 1+(
        SELECT count(*) 
        from AppBundle\Entity\User a 
        WHERE a.points > b.points) as RNK, points 
    FROM AppBundle\Entity\User b
");

But this return a syntax error : "[Syntax Error] line 0, col 14: Error: Expected Literal, got 'SELECT'"

2 Answers 2

0

try this :

$query = $this->getEntityManager()->createQuery("
SELECT b.id, 1+(
    SELECT count(a) 
    from AppBundle\Entity\User a 
    WHERE a.points > b.points) as RNK, points 
FROM AppBundle\Entity\User b");
0

Because you want to get the number of the same entity (USER), why not simply do :

$query = $this->getEntityManager()->createQuery("
    SELECT u.id, 1+count(u.*) as RNK, u.points FROM AppBundle\Entity\User u
");

from the doc : doctrine chap 14.2.4 :

$query = $em->createQuery('SELECT COUNT(u.id) FROM Entities\User u');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.