Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top