DQL Query with LEFT JOIN and COUNT

I'm just working on a project (Symfony2) with two entities with Many-to-Many ORM connection. Right now, I am driving mad on this DQL Query:

SELECT p, COUNT(s.id) AS countSup FROM AapFrontendBundle:Paper p LEFT JOIN p.supervisors s WHERE (countSup = 0) GROUP BY p.id

with SQL output:

SELECT p0_.id AS id0,
p0_.title AS title1,
p0_.description AS description2,
p0_.research_questions AS research_questions3,
p0_.basic_literature AS basic_literature4,
p0_.student AS student5, p0_.status AS status6,
p0_.category AS category7,
p0_.type AS type8,
p0_.hash AS hash9,
COUNT(u1_.id) AS sclr10,
p0_.insertBy AS insertBy11

FROM paper p0_
LEFT JOIN supervisors s2_
 ON p0_.id = s2_.paperId
LEFT JOIN user u1_
 ON u1_.id = s2_.userId

WHERE (sclr10 = 0) GROUP BY p0_.id

And got the error message

Column not found: Unknown column 'sclr10' in 'where clause' 

The table "supervisors" is used for the many-to-many-relationship between papers and users. Basically, I want to use this query in the paper-repository to find all paper entities without linked user as supervisors.

Answers


You can use the GROUP BY / HAVING combination:

SELECT ...

FROM paper p0_
LEFT JOIN supervisors s2_
 ON p0_.id = s2_.paperId
LEFT JOIN user u1_
 ON u1_.id = s2_.userId

GROUP BY p0_.id
HAVING COUNT(u1_.id) = 0

Need Your Help

Java- Issue with opening and Scanning file

java bufferedreader inputstreamreader

I am writing a program in Java, it scans a file, counts lines, character, palindromes, words. My problem is when I ask for a filename, I am using BufferedReader and InputStreamReader to scan the fi...

Check if bookmarklet is ran on the page in Chromium

javascript google-chrome-extension bookmarklet

There is a web forum which I visit on daily basis where like option is implemented. When user click on the like button, bookmarklet is executed and number of likes is automatically changed. What I ...