0

我是cakephp的初学者,我的版本是2.4.3

在文档中,我在下面找到示例代码

public function index() {
    try {
        $this->Paginator->paginate();
    } catch (NotFoundException $e) {
        //Do something here like redirecting to first or last page.
        //$this->request->params['paging'] will give you required info.
    }
}

我的问题 :

1.如何重定向到最后一页,有什么办法可以得到总页数?

2.我试图用debug()输出$this->request->params['paging'],但是什么也没显示,只是一个空值,我做错了什么吗?

请帮助我,认为

4

2 回答 2

1

在 CakePHP 3.4 我有这个解决方案:

$page  = (!empty($this->request->query['pagina']) ? $this->request->query['pagina'] : 1);
    $this->paginate = ['page' => $page, 'limit' => '10'];

    try {

        $products = $this->paginate($products);

    } catch (NotFoundException $e) {

        $this->paginate = ['page' => $page-1, 'limit' => '10'];
        $products = $this->paginate($products);

    }
于 2017-07-04T11:18:09.410 回答
0

数据不可用要么是错误,要么是文档错误 - 我会说是前者,因为当组件已经这样做paging时再次计算记录会有点多余。Paginator

查看负责的代码:

https://github.com/cakephp/cakephp/blob/2.4.3/lib/Cake/Controller/Component/PaginatorComponent.php#L215

表明在数组paging中设置键之前抛出异常。params因此,在解决此问题之前,您要么必须修改核心,要么自己重新计算和计算,如下所示(可能需要一些调整):

public function index()
{
    try
    {
        $this->Paginator->paginate();
    }
    catch(NotFoundException $e)
    {
        extract($this->Paginator->settings);
        $count = $this->ModelName->find('count', compact('conditions'));
        $pageCount = intval(ceil($count / $limit));
        $this->redirect(array('page' => $pageCount));
    }
}
于 2013-11-28T14:47:14.523 回答