Home  >  Article  >  PHP Framework  >  How to sort yii2.0 tables

How to sort yii2.0 tables

尚
Original
2019-12-09 13:37:132340browse

How to sort yii2.0 tables

In the actual development of Yii2.0 projects, we often encounter the use of the sorting function that comes with Yii2.0.

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:

1. attributes specifies the attributes according to 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.

2. attributeOrders gives the sorting direction currently set for each attribute.

3. 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.

(Recommended learning: yii framework)

The following is the specific method of using the sorting function:

1. Set the sorting rules

Pay attention to the introduction of the Sort class, such as: use yii\data\Sort;

// 设置排序字段
        $sortObject = new Sort([
                'sort' => $sort,
                'defaultOrder' => ['id' => SORT_DESC],
                'attributes' => [
                        'id' => [
                                'asc' => ['id' => SORT_ASC],
                                'desc' => ['id' => SORT_DESC],
                        ],
                        'user_name' => [
                                'asc' => ['ecs_users.user_name' => SORT_ASC],
                                'desc' => ['ecs_users.user_name' => SORT_DESC],
                        ],
                        'add_time' => [
                                'asc' => ['add_time' => SORT_ASC],
                                'desc' => ['add_time' => SORT_DESC],
                        ],
                ],
        ]);

2. Use the paging function of Yii2.0 and add sorting

// 处理分页
        $queryClone = clone $query;
        $totalCount = $queryClone->count();
        $pages = new Pagination(['defaultPageSize'=>$rows,'totalCount'=>$totalCount,'pageSizeLimit'=>false]);
        $query = $query->offset($offset)
                       ->limit($pages->limit)
                       ->orderBy($sortObject->orders)
                       ->asArray()
                       ->all();

3. Modify the bottom layer of Yii2.0 sorting Code

The underlying corresponding source code: vendor/yiisoft/yii2.0/data/Sort.php

1. Add an attribute to the Sort class: public $sort;

2 .Add the following code to about line 249 of the source code: (implement post to obtain paging parameters)

  // get请求获取数组为空,则调用自定义属性sort。用于满足post获取后处理自定义参数
 if(!isset($params[$this->sortParam])){
    $params[$this->sortParam] = $this->sort;
  }

3. Receive paging parameters in the controller, as follows:

$sort = $_POST['sort'] ? strval($_POST['sort']) : 'id';
$sort = $_POST['order'] == 'asc' ? $sort : '-'.$sort;

Note: id in Yii2.0 It means sorting in positive order by id, and -id means sorting in reverse order by id. This is also the reason why statement 2 above is handled in this way.

The above is the detailed content of How to sort yii2.0 tables. 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