订单不适用于sortWhitelist

时间:2017-10-20 11:16:51

标签: cakephp-3.0

使用CakePHP 3.5.3

您好,

以下代码的工作正常,并显示结果集并通过due_date asc对其进行排序。

public $paginate = [        
    'limit' => 5,
    'order' => [
        'Activities.due_date' => 'asc'
    ]
];   

public function index()
{        
    $session = $this->request->session();

    // Declare client id 1
    $cidOne = null;
    $cidOne = $session->read('Cid.one');

    // NOTE* DON'T USE ORDER HERE BECAUSE THE SORTS WILL NOT WORK
    $query = $this->Activities->find('all')
    ->where(['cid_1' => $cidOne])
    ->andWhere(['status' => 1]);

    // Send the query to the view.
    $this->set('activities', $this->paginate($query));
}

但是当我在下面添加sortWhitelist时,初始页面加载没有按due_date排序,也没有显示排序箭头。

public $paginate = [
    'sortWhitelist' => [
       'due_date', 'related_to', 'subject', 'post_code', 'type', 'priority'
    ],
    'limit' => 5,
    'order' => [
        'Activities.due_date' => 'asc'
    ]
];

感谢您的帮助。 ž。

1 个答案:

答案 0 :(得分:0)

sortWhitelist数组,order数组和paginator链接之间必须保持一致

因此,如果您的字段为Activities.due_date,则您的代码将变为:

public $paginate = [
    'sortWhitelist' => [
       'Activities.due_date', 'related_to', 'subject', 'post_code', 'type', 'priority'
    ],
    'limit' => 5,
    'order' => [
        'Activities.due_date' => 'asc'
    ]
];

并在您的视图中

<?= $this->Paginator->sort('Activities.due_date', __('Due Date')) ?>

如果字段名称中没有歧义,您可以省略模型名称,只需使用due_date作为列名称