Doctrine 2 Order on the values ​​of ASC and Null in the latter

I am trying to get a result that I need to sort in ascending order. But some values ​​will be empty / empty, I need them to be the last, and the sorting starts at 0 1 2 and then null.

I tried SortableNullsWalker but that didn't help. The value that I am sorting is not a column, its multiplying by two values ​​that are sorting, which is why I believe SortableNullsWalker is not working. Any help please

$dql = 'SELECT (column1 * column2) as distance FROM table)

                ORDER BY distance ASC ';

        $dq = $this->getEntityManager()->createQuery($dql);

The result is obtained as '', '', 0, 1, 2.334, ....

But I'm trying to do it like: 0, 1, 2.334,......, '', ''

+4
source share
2 answers

This is a similar solution that works with non-numeric columns / expressions:

/* @var $datasource QueryBuilder */  
$datasource->addSelect('CASE WHEN xxx.yyy IS NULL THEN 1 ELSE 0 END as HIDDEN yyy_is_null');
$datasource->orderBy('yyy_is_null', 'ASC'); // always ASC
$datasource->addOrderBy('xxx.yyy','DESC'); //DESC or ASC
+1

, , minus(-) DESC order

$dql = 'SELECT (column1 * column2) as distance,
        -(column1 * column2) as HIDDEN distance1 
        FROM table
        ORDER BY distance1 DESC';

        $dq = $this->getEntityManager()->createQuery($dql);
+6

Source: https://habr.com/ru/post/1538944/


All Articles