dssqq64884 2015-01-21 16:21
浏览 37

当使用createUrl()时,Yii 2在排序名称的开头添加连字符

I'm using Yii 2 and I have created a Pagination and Sort system and have the following code:

Here is the generalized code:

$sql = $this->db->createCommand("SELECT COUNT(*) FROM some_table WHERE some_id=:some_id");
$sql->bindValue(':some_id', $this->some_id);
$count = $sql->queryScalar();

$dataProvider = new SqlDataProvider([
    'sql' => 'SELECT * FROM some_table WHERE some_id=:some_id',
    'params' => [':some_id' => $this->some_id],
    'totalCount' => $count,
    'sort' => [
        'attributes' => [
            'sort_way_1' => [
                'asc' => ['col_1' => SORT_DESC, 'col_2' => SORT_ASC, 'col_3' => SORT_ASC],
                'desc' => ['col_1' => SORT_DESC, 'col_2' => SORT_DESC, 'col_3' => SORT_DESC],
                'default' => SORT_DESC,
                'label' => 'Sort Way 1',
            ],
            'sort_way_2' => [
                'asc' => ['col_1' => SORT_DESC, 'col_4' => SORT_ASC, 'col_3' => SORT_ASC],
                'desc' => ['col_1' => SORT_DESC, 'col_4' => SORT_DESC, 'col_3' => SORT_DESC],
                'default' => SORT_DESC,
                'label' => 'Sort Way 2',
            ],
            'sort_way_3' => [
                'asc' => ['col_1' => SORT_DESC, 'col_5' => SORT_ASC, 'col_3' => SORT_ASC],
                'desc' => ['col_1' => SORT_DESC, 'col_5' => SORT_DESC, 'col_3' => SORT_DESC],
                'default' => SORT_DESC,
                'label' => 'Sort Way 3',
            ],
            'sort_way_4' => [
                'asc' => ['col_1' => SORT_DESC, 'col_6' => SORT_ASC, 'col_3' => SORT_ASC],
                'desc' => ['col_1' => SORT_DESC, 'col_6' => SORT_DESC, 'col_3' => SORT_DESC],
                'default' => SORT_DESC,
                'label' => 'Sort Way 4',
            ],
        ],
        'enableMultiSort' => false,
        'defaultOrder' => ['sort_way_1' => SORT_DESC],
    ],
    'pagination' => [
        'defaultPageSize' => $this->per_page,
        'pageSize' => $this->per_page,
        'pageSizeLimit' => [5,100],
        'pageSizeParam' => 'per_page',
        'totalCount' => $count,
    ],
]);

Now for my sorting links I attempted to create the URL's with the createUrl method and everything works fine, except for the fact it appends a - to the start of the sort attribute name in the URL (apart from the first sort method).

Example:

http://example.com/some_dir/?page=1&sort=sort_way_1 (good)
http://example.com/some_dir/?page=1&sort=-sort_way_2 (bad)

I then tried doing it with the LinkSorter Widget but I got the same results.

I dug into the code a bit and found the below code inside the sort.php createSortParam method:

foreach ($directions as $attribute => $direction) {
    $sorts[] = $direction === SORT_DESC ? '-' . $attribute : $attribute;
}

This appears to be the culprit for the appended hyphen, but I'm not sure why it does it or why it doesn't add it to the first sorting attribute.

Does anyone have any more info on this or how to get around it? As with the appended hyphens the requested sorting method is coming back as invalid.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题