0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.