Help us understand the problem. What is going on with this article?

cakephp 2.x ページング機能

More than 1 year has passed since last update.

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>
Youz
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした