My concrete problem in a Symfony2 - Doctrine application:
I want to order search results of a query in a self defined order (w.winnerType: 'main','special', 'normal', 'web) which is NOT ASC or DSC,
I've tried several versions as proposed in ORDER BY the IN value list (link to Stackoverflow)
Version 1
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery (
"select w
FROM MyBundle:Winner w
WHERE
w.winnerType IN ('main', 'special', 'normal', 'web')
.
ORDER BY w.winnerType='main' DESC, w.winnerType='special' DESC,
w.winnerType='normal' DESC, w.winnerType='web' DESC
") ;
This gives the error message:
[Syntax Error]: Error Expected end of string, got '='
Version 2
ORDER BY FIELD (w.winnerType, 'main','special', 'normal', 'web')
Gives the error message
[Syntax Error] Error: Expected known function, got 'FIELD'
Version 3
ORDER BY CASE w.winnerType
WHEN 'main' THEN 1
WHEN 'special' THEN 2
WHEN 'normal' THEN 3
WHEN 'web' THEN 4
ELSE 999
Gives the error message
QueryException: [Syntax Error] line 0, col 314: Error: Expected end of string, got 'w'
Debug Info: CRITICAL - Uncaught PHP Exception Doctrine\ORM\Query\QueryException: "[Syntax Error] line 0, col 314: Error: Expected end of string, got 'w'" at C:\Users\sDev2\xampp\htdocs\bvbp\vendor\doctrine\orm\lib\Doctrine\ORM\Query\QueryException.php
None of the standard SQL custom sort methods (posted in several stackoverflow items) works in my symfony/doctrine environment.
If I use the "ASC" parameter (which does not solve my problem): I don't have problems in my Symfony application
Example (working without problems)
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery (
"select w
FROM MyBundle:Winner w
WHERE
w.winnerType IN ('main', 'special', 'normal', 'web')
.
ORDER BY w.winnerType ASC
") ;
What is the syntax to be used in symfony2 / doctrine? Thanks