L
L
Leopandro2016-07-10 20:53:05
Yii
Leopandro, 2016-07-10 20:53:05

How to set advanced sorting?

I need to sort a string as a number. For this I use:

select * from `panel` order by cast(town as unsigned) asc LIMIT 20

How to write it in $dataProvider? taking into account that asc or desc can come, i.e. some rule needs to be entered somewhere.
By default, there is only this sorting:
'town' => [
          'asc' => ['town' => SORT_ASC],
          'desc' => ['town' => SORT_DESC]
        ],

In general, I need to somehow write a rule so that if where 'asc' => 'string' is, then this string would become an Expression for this command, for example
'town' => [
          'asc' => 'cast(town as unsigned) asc',
          'desc' => 'cast(town as unsigned) desc
        ],

How to write it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita, 2016-07-10
@Leopandro

'town' => [
          'asc' => ['cast(town as unsigned)' => SORT_ASC],
          'desc' => ['cast(town as unsigned)' => SORT_DESC]
        ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question