CakePHP 2.6.10 ページング機能と検索機能

#１．publicにcoponentsを作成

```ruby:Controller
public $components = array('Paginator');
```

<img width="393" alt="1__maxuser01_maxtestweb01__home_maxuser01_htdocs_buhinmax_app__ssh_.jpg" src="https://qiita-image-store.s3.amazonaws.com/0/188645/cfe366f4-77c3-5b42-5986-03e17c5c7c1c.jpeg">


#２．条件設定と書き出し

```ruby:Controller
// フィールド指定
$paginate['fields'][] = 'Orders.*, OrderDetails.*, Members.member_flg, Members.pref';

// Join設定
$paginate['joins'][] = array(
  'type' => 'Inner',
  'table' => 'order_detail',
  'alias' => 'OrderDetails',
  'conditions' => 'Orders.order_no = OrderDetails.order_no',
);
$paginate['joins'][] = array(
  'type' => 'Inner',
  'table' => 'Member_list',
  'alias' => 'Members',
  'conditions' => 'Members.member_id = Orders.login_id',
);

//表示数指定
$paginate['limit'] = 20;

//ソート設定
$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']);
}
$this->set('Orders', $orders);
```

## その他検索条件
```ruby:Controller
// 検索条件
if(!empty($_GET['mode']) && $_GET['mode'] == 'search'){
　// ここに条件を入れていく

  // 期間指定 //Orders.order_date を書き換え
  if(!empty($_GET['date_before']) && !empty($_GET['date_after'])){
    $option['conditions']['Orders.order_date BETWEEN ? AND ?'] = array($_GET['date_before']." 00:00:00", $_GET['date_after']." 23:59:59");
    $getform['date_before'] = $_GET['date_before'];
    $getform['date_after'] = $_GET['date_after'];
  }elseif(!empty($_GET['date_before']) && empty($_GET['date_after'])){
    $option['conditions']['Orders.order_date >='] = $_GET['date_before']." 00:00:00";
    $getform['date_before'] = $_GET['date_before'];
  }elseif(empty($_GET['date_before']) && !empty($_GET['date_after'])){
    $option['conditions']['Orders.order_date <='] = $_GET['date_after']." 00:00:00";
    $getform['date_after'] = $_GET['date_after'];
  }

  // フリーワード
  if(!empty($_GET['keyword'])){
    $keys = preg_split('/[\s|\x{3000}]+/u', $_GET['keyword']);
    foreach($keys as $key => $value){
      $option['conditions'][$key]['OR']['Orders.order_no like'] = "%".$value."%";
      $option['conditions'][$key]['OR']['Orders.campany_name like'] = "%".$value."%";
      $option['conditions'][$key]['OR']['Orders.order_name_sei like'] = "%".$value."%";
    }
  }
  $getform['keyword'] = $_GET['keyword'];

}
```

#3．フォームを作る

```ruby:HTML
<!-- 検索機能 -->
<form method="get" name="search" id="search">

<div>
  <input type="hidden" name="mode" value="search">
  <h3>検索項目</h3>

  <span>登録期間：
    <input type="date" name="date_before" <?php if(!empty($getform['date_before'])){ echo 'value="'.$getform['date_before'].'"';} ?>>
     -
    <input type="date" name="date_after" <?php if(!empty($getform['date_after'])){ echo 'value="'.$getform['date_after'].'"';} ?>>
  </span>

  <span>キーワード：
    <input type="text" name="keyword" value="<?php if(!empty($getform['keyword'])){ echo $getform['keyword']; } ?>">
  </span>

  <input type="submit" value="検索">
</div>

</form>
```

#3．ページング機能を作る

```ruby:HTML
<!-- ページング -->
<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>
```