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. $this->request->params['paging'] をdebug()で出力しようとしたのですが何も表示されず、null値だけでした。

私を助けてください、と思います

4

2 に答える 2

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 に答える