Major Productions
Major Productions

Reputation: 6062

Doctrine2 - Semantical error, even though the column in the error exists

I'm getting the following error:

[Semantical Error] line 0, col 15 near 'question_id)': Error: Class Acme\MyBundle\Entity\SurveysQuestions has no field or association named question_id

I'm looking at SurveysQuestions in PhpMyAdmin, and, yes, it has a question_id field:

survey_id       question_id       score
7               1                 3.5
7               2                 4

etc.

The query is:

'SELECT COUNT(sq.question_id) AS votes, sq.score AS score, q.question FROM MyBundle:SurveysQuestions sq LEFT OUTER JOIN sq.question q WHERE q.id = :id AND q.deletedAt IS NULL GROUP BY q.id, sq.score ORDER BY sq.score DESC'

Not sure what I'm doing wrong....

Upvotes: 0

Views: 72

Answers (1)

K. Norbert
K. Norbert

Reputation: 10684

You are writing SQL, but Doctrine expects DQL, you should read up on the chapter Doctrine Query Language.

Two problems that I see from a glance in your query:

  • You are not supposed to query on table columns (sq.question_id), you have to query on class properties. The sq.question_id column is a foreign key, which does not exist on your class, you probably want to use q.id.

  • You are selecting a COUNT(), and sq.score field, which is going to return an array, instead of an object, because you are selecting specific fields, instead of an entity. If you are expecting an array, that's fine, but if you want an object you have to write "SELECT COUNT(q.id) as votes, sq ...". If you only want these two fields, but still receive an object, you have to use the partial syntax "SELECT PARTIAL COUNT(q.id), sq.{score}"

  • I'm unsure if Doctrine accepts LEFT OUTER JOIN, I think it only accepts LEFT JOIN (which does the same), but I'm not 100% sure on this.

Upvotes: 1

Related Questions