Home >PHP Framework >YII >What are the sorting methods in yii
When displaying multiple pieces of data, it is usually necessary to sort the data according to user-specified columns. Yii uses the yii\data\Sort object to represent information about the sorting scheme. In particular:
attributes: Specifies the attributes by which the data is sorted. An attribute can be a simple model attribute, or a composite attribute that combines multiple model attributes or DB columns. More details are given below:
attributeOrders: Gives the ordering direction currently set for each attribute.
orders: Gives the sorting direction according to the low-level column.
To use yii\data\Sort, you must first declare what attributes can be sorted. Then get the currently set sorting information from attributeOrders or orders, and then use them to customize data queries. For example:
use yii\data\Sort; $sort = new Sort([ 'attributes' => [ 'age', 'name' => [ 'asc' => ['first_name' => SORT_ASC, 'last_name' => SORT_ASC], 'desc' => ['first_name' => SORT_DESC, 'last_name' => SORT_DESC], 'default' => SORT_DESC, 'label' => 'Name', ], ], ]); $articles = Article::find() ->where(['status' => 1]) ->orderBy($sort->orders) ->all();
PHP Chinese website has a large number of free Yii introductory tutorials, everyone is welcome to learn!
The above is the detailed content of What are the sorting methods in yii. For more information, please follow other related articles on the PHP Chinese website!