Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
Doctrine2 error "Expected known function, got 'COUNT'" when in order by clause

I'm using doctrine2

I try to get the number of "Events" ordered by number of tickets sold

$manager = $this->getDoctrine()->getManager();
$builder = $manager->createQueryBuilder();

return $builder
    ->select('e')
    ->from('AppBundle:Event', 'e')
    ->leftJoin('e.tickets', 't')
    ->orderBy('COUNT(t)')
    ->groupBy('e.id')
    ->setMaxResults(10)
    ->getQuery()
    ->getResult()
;

This produce the error

[Syntax Error] line 0, col 80: Error: Expected known function, got 'COUNT'

With the following DQL produce

QueryException: SELECT e FROM AppBundle:Event e LEFT JOIN e.tickets t GROUP BY e.id ORDER BY COUNT(t.id) ASC

However if I put the COUNT in the SELECT part, it does work correctly (but then I have to do additional treatment to remove the unwanted field of the SELECT)

return $builder
    ->select('e')
    ->from('AppBundle:Event', 'e')
    ->addSelect('COUNT(t) AS nbrTicketsSold')
    ->leftJoin('e.t', 't')
    ->orderBy('nbrTicketsSold')
    ->groupBy('e.id')
    ->setMaxResults(10)
    ->getQuery()
    ->getResult()
;

Is using COUNT in an ORDER BY clause a known limitation of DQL ?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The EBNF in the doctrine docs actually state what's possible in the ORDER BY clause:

OrderByClause       ::= "ORDER" "BY" OrderByItem {"," OrderByItem}*
OrderByItem ::= (SimpleArithmeticExpression | SingleValuedPathExpression | ScalarExpression | ResultVariable | FunctionDeclaration) ["ASC" | "DESC"]

With FunctionDeclaration containing these functions:

FunctionsReturningNumerics: LENGTH LOCATE ABS SQRT MOD SIZE DATE_DIFF BIT_AND BIT_OR

FunctionsReturningDateTime: CURRENT_DATE CURRENT_TIME CURRENT_TIMESTAMP DATE_ADD DATE_SUB

FunctionsReturningStrings: CONCAT SUBSTRING TRIM LOWER UPPER IDENTITY

COUNT is part of the AggregateExpression group (Link), those are not allowed in the ORDER BY clause.

Furthermore, the docs actually have an example further up using AS HIDDEN to order by count:

Retrieve user entities with Arithmetic Expression in ORDER clause, using the HIDDEN keyword:

$query = $em->createQuery('SELECT u, u.posts_count + u.likes_count AS HIDDEN score FROM CmsUser u ORDER BY score');
$users = $query->getResult(); // array of User objects
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!