5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

cakePHP2系列でページネーションを行うときに注意すべき点

Posted at

要点

paginatorComponentを利用する。
つまり、$this->Paginator 経由でやりくりする。

前提

本記事で取り扱うcakePHPのバージョンはcakePHP2系列の話です。

注意点

cakePHPにはページネーションの実装が2つ存在します。

  1. (おそらく)Controllerへの実装
  2. Componentへの実装


これらの違いを意識してサンプルコードを読まないと、思ったようにコードが動かずに頭を悩ませます。

対策

新しい実装はComponentの方の実装なのでそれを利用します。

Pagination cakePHP2 cookbook

以上から適当に抜粋して利用方法を記載します。

準備

ページネーションを利用したいコントローラー内でPaginatorコンポーネントの利用を宣言します。

public $components = array('Paginator');

設定と利用

データ取得の設定は以下のように行います。

public function index(){
    $this->Paginator->setting = array('必要な設定');
    $this->set('page',$this->Paginator->paginate('対象モデル'));
}

また複数のモデルに対してページネーションを行いたい場合は

public function index(){
    $this->Paginator->setting['対象モデル1'] = array('必要な設定');
    $this->Paginator->setting['対象モデル2'] = array('必要な設定');
    
    $this->set('page1',$this->Paginator->paginate('対象モデル1'));
    $this->set('page2',$this->Paginator->paginate('対象モデル2'));
}
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?