I need to sort the data from the database. For example, there is data: 1,2, A, 1C, 1A, B, 3,10, after applying the sorting I expect to see: 1,1A, 1C, 2,3,10, A, B.
Once I already had such a task, but without using the framework. I decided this with this query:

SELECT * FROM table ORDER BY Name=0,-Name DESC, Name 

The request is working and gives the desired result. How to write such a query using querybuilder? I manage to sort it like this: A, B, 1,1A, 1C, 2,3,10. With this request:

 SELECT t FROM table t ORDER BY t.name+0 ASC, t.name 

But if you put an equal sign or a minus in the sorting condition, I get an error. Therefore I can not sort the data in any way.
Tell me, how can I solve this problem?

1 answer 1

Alternatively, if for some reason there is no possibility / I don’t want to use native SQL, you can use the Query hints mechanism - t / e to tell the doctrine how to interpret this or that DQL construction. For these purposes, you must implement the appropriate SqlWalker , namely the walkOrderByClause method. When getting the results, you should specify the name of hint for query:

 $query = $ORMQueryBuilder->getQuery(); $query->setHint($yourHintName, $yourHintParams); return $query->getResult(); 

After the implementation of this class it will be possible to insert into DQL your construction in ORDER BY .