cakephp:添加后按ID desc排序的索引

时间:2018-07-09 14:46:17

标签: php cakephp cakephp-3.x

在我的应用程序中,默认情况下,所有索引视图均按名称ASC排序。 仅在添加新行之后,我才需要按ID desc排序数据,以帮助用户查看索引顶部最后添加的行。

在控制器中,添加功能:

if ($this->Assets->save($asset)) {
            $this->Flash->success(__('The asset has been saved.'));
            //die('new id:' . $asset->id);
            return $this->redirect(['action' => 'index']);
        }

此尝试无效:

return $this->redirect(['action' => 'index', 'order' => ['id' => 'DESC']]);

我该怎么办?

1 个答案:

答案 0 :(得分:1)

您是否为视图启用了分页?

如果是这样,正确的链接应该是

return $this->redirect([
    'action' => 'index', 
    '?' => [
        'sort' => 'id',
        'direction' => 'desc'
    ]
]);