LoginSignup
2
2

More than 5 years have passed since last update.

cakephp 2.x ページング機能

Posted at

cakephpのマニュアルを解読するのは結構疲れるので、自分でわかりやすく記載しておきます。
他の人も参考になれば。

1.コントローラーにcomponentsを追加
基本的にはpublic $uses = array(public function index() {の間でいいと思います。

xxxController.php
public $components = array('Paginator');

2.SQLでデータ取得時の書き方
基本的にはpaginateでfiledsの設定やjoinsを設定します。
その前で検索条件を$option['conditions']で作っておき、取得時に指定してあげます。
$this->Paginator->settings = $paginate;で設定を保存してから検索をかけるのですが、try&catchを使い再検索時に現在のページ位置を1に戻すようにしています。

xxxController.php
$paginate['fields'][] = 'Orders.*, OrderDetails.*, Members.member_flg, Members.pref';
$paginate['joins'][] = array(
 'type' => 'Inner',
 'table' => 'N_order_detail',
 'alias' => 'OrderDetails',
 'conditions' => 'Orders.order_no = OrderDetails.order_no',
);

$paginate['joins'][] = array(
 'type' => 'Inner',
 'table' => 'N_Member_list',
 'alias' => 'Members',
 'conditions' => 'Members.member_id = Orders.login_id',
);

$paginate['limit'] = 30;
$paginate['order']['order_date'] = "desc";

$this->Paginator->settings = $paginate;
try{
 $orders = $this->paginate('Orders', $option['conditions']);
}catch(Exception $e){
 $this->request->params['named']['page'] = 1;
 $orders = $this->paginate('Orders', $option['conditions']);
}

3.ctpにページングを機能を付ける
好きなところに下記を記載するだけ。送り文字変えたりいろいろできるみたいですが、それは各自で調べてください。

index.ctp等
<!-- ページング -->
<div id="paginate">
  <span class="prev"><?php echo $this->Paginator->prev('< 前へ', array(), null, array('class' => 'prev disabled')); ?></span>
  <span class="pageno"><?php echo $this->Paginator->numbers(array('separator' => '')); ?></span>
  <span class="next"><?php  echo $this->Paginator->next('次へ >', array(), null, array('class' => 'next disabled')); ?></span>
</div>
2
2
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
2
2