cosmicsafari
cosmicsafari

Reputation: 4049

Order by FIELD() using doctrine, is it possible?

I am working on a project which is built using Symfony and Doctrine, however I am still new to the whole setup.

Cut a long story short I have need to order a result set by the order of the array used in the select.

Which I believe could be done using the FIELD() function, however from my investigations it doesn't look like its possible using Doctrine.

For example, I am stumped as how I can utilise FIELD() in a query like below (if its even possible):

return $this
            ->createQueryBuilder('a')
            ->where('a.identifier IN (:identifiers)')
            ->setParameter('identifiers',$identifiers)
            ->getQuery()
            ->getResult();

Upvotes: 5

Views: 12316

Answers (2)

CN Lee
CN Lee

Reputation: 134

I'm using doctrine v2.5.x. It does not support the FIELD function.

I found and used this file. https://github.com/beberlei/DoctrineExtensions/blob/master/src/Query/Mysql/Field.php

Or you can install the extension.

Upvotes: 0

Sreenadh Tg
Sreenadh Tg

Reputation: 146

I am using doctrine version 1.2.4 and using following query to order by specific values.

$query->orderBy('FIELD(str,str1,str2,str3,...)');

Upvotes: 4

Related Questions