user1014102
user1014102

Reputation: 345

Symfony2 doctrine custom function

Hi I like to add a custom function to doctrine.

I need to add a mysql Field function to be able to order by size like here: mysql custom order by with mixed data types

So I use this as exemple: https://github.com/beberlei/DoctrineExtensions/blob/master/lib/DoctrineExtensions/Query/Mysql/Field.php

and try to add it to Symfony2 like that: http://symfony.com/fr/doc/current/cookbook/doctrine/custom_dql_functions.html

When I do:

$queryBuilder->addOrderBy("FIELD(size, 'XS', 'S', 'M', 'L', 'XL', 'XXL', 'XXXL'), size, length", 'ASC');

I always get the error: Error: Expected end of string, got '('

Any idea how to implement it?

Upvotes: 0

Views: 797

Answers (1)

Crozin
Crozin

Reputation: 44376

You cannot use DQL functions within ORDER BY clause. You must select the result of FIELD() function into hidden field and sort results using that field:

 SELECT ..., FIELD(size, ...) AS HIDDEN sizeOrder
 FROM ...
 ORDER BY sizeOrder

sizeOrder field won't affect your results as it won't be even hydrated.

Upvotes: 1

Related Questions