为什么我会得到';调用未定义的方法PaginatorComponent::sort()';在CakePHP2.x中?

为什么我会得到';调用未定义的方法PaginatorComponent::sort()';在CakePHP2.x中?,php,cakephp,pagination,cakephp-2.0,cakephp-2.x,Php,Cakephp,Pagination,Cakephp 2.0,Cakephp 2.x,我刚开始学习PHP,cakePHP,我无法对我的页面进行排序。 我的控制器如下 public $helpers = array('Html', 'Form', 'Flash', 'Paginator'); public function index() { $this->Post->recursive = 0; $this->paginate = array ('limit' => 5); $this->set('posts', $this

我刚开始学习PHP,cakePHP,我无法对我的页面进行排序。 我的控制器如下

public $helpers = array('Html', 'Form', 'Flash', 'Paginator');
public function index() 
{
    $this->Post->recursive = 0;
    $this->paginate = array ('limit' => 5);
    $this->set('posts', $this->paginate());
    $this->set('posts', $this->Post->find('all'));
    echo $this->Paginator->sort('id');
}
Paginate工作正常,但对于sort,我得到一个致命错误,如下所示

调用未定义的方法PaginatorComponent::sort()

也找不到与错误相关的任何内容。这可能是非常基本的,但它将真正帮助我学习更多,我想学习更多!任何帮助都将不胜感激。 提前谢谢你

更新:

我在索引视图中添加了以下代码

<th><?php echo $paginator->sort('Id', 'id'); ?></th>

但是现在我得到了以下错误
在null上调用成员函数sort()

我相信您正在尝试设置分页默认值。正确的方法是在控制器中定义:

public $paginate = array(
    'order' => array(
        'Post.id' => 'desc'
    )
);
或在行动中:

$this->paginate = array (
   'limit' => 5,
   'order' => array(
        'Post.id' => 'desc'
    )
);
您的索引操作应如下所示:

public function index() {
    $this->Post->recursive = 0;
    $this->paginate = array (
       'limit' => 5,
       'order' => array(
           'Post.id' => 'desc'
       )
    );
    $this->Paginator->settings = $this->paginate;
    $this->set('posts', $this->paginate());
}
在您看来,CakePHP2.x的正确语法是

<?php echo $this->Paginator->sort('id'); ?>

在我的索引()中

稍后在我的索引视图中,我使用了

<?php echo $this->Paginator->sort('id','Id'); ?>

语法:this->Paginator->sort('column name','label name')


而不是普通的html代码。成功了

您是否为分页设置了
$components
?在我的AppController中,我有公共$components=array('Flash','Session','Auth','Paginator','Security');是的,谢谢你。。。我在视图方面遇到了一些问题,但经过进一步的研究后,我发现了这一点:)我很高兴你让它工作起来了!
Paginator::sort()
的第二个参数是可选的,可以省略(它默认为键的屈折形式),如..所示。我编辑了您的问题以提高其可搜索性。
<?php echo $this->Paginator->sort('id','Id'); ?>