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

时间:2015-01-21 16:21:27

标签: php yii2

我正在使用Yii 2,我创建了一个PaginationSort系统并拥有以下代码:

这是通用代码:

$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,
    ],
]);

现在,对于我的排序链接,我尝试使用createUrl方法创建URL,并且一切正常,除了它将-附加到URL中排序属性名称的开头这一事实(除了第一种排序方法)。

示例:

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

然后我尝试使用LinkSorter Widget,但我得到了相同的结果。

我挖了一下代码,在sort.php createSortParam方法中找到了以下代码:

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

这个出现是附加连字符的罪魁祸首,但我不确定它为什么会这样做,或者为什么它不将它添加到第一个排序属性。

有没有人有关于此的更多信息或如何绕过它?与附加的连字符一样,请求的排序方法将返回为无效。

0 个答案:

没有答案