Home > PHP Framework > YII > body text

What are the sorting methods in yii

Release: 2019-12-17 16:18:54
Original
3170 people have browsed it

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();
Copy after login

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!

Related labels:
yii
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!