Home  >  Article  >  PHP Framework  >  What are the sorting methods in yii

What are the sorting methods in yii

尚
Original
2019-12-17 16:18:543041browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn