CakePHP1.2 RC2 Paginate で検索条件をURLで引き継ぐ
今、1.1で作ったものを1.2RC2に変更しています。
その中で気づいた事の覚え書きってことで
ページまわりは特に違うので
(そもそも1.1にはありませんが・・・)
検索条件なんかは、セッション使ってもいいんだけど、
携帯のことを考えると(てか、ドコモ)URLで次のページに引き継ぎたい。
controllers/users_controller.php
<?php class UsersController extends AppController { public $name = 'Users'; function index() { $this->paginate = array( 'recursive' => -1, ); // 基本条件 $conditions = array( 'User.hidden' => 0, ); $users = $this->paginate('User', $this->_setConditions($conditions)); $types= $this->User->types; $this->set(compact('shops', 'types')); } function _setConditions($conditions = array()) { $default = array( 'type' => 99, ); // post時のリセットするパラメータ $vars = array('page'); if (!empty($this->data['User'])) { $this->passedArgs = am($this->passedArgs, $this->data['User']); foreach ($vars as $key) { unset($this->passedArgs[$key]); } } // 初期値とマージ $this->data['User'] = am($default, $this->passedArgs); if (!empty($this->passedArgs['type'])) { if (99 === $this->passedArgs['type']) { // すべて表示を選択したとき unset($this->passedArgs['type']); } else { $conditions = am($conditions, array('User.type' => $this->passedArgs['type'])); } } return $conditions; } }
views/users/index.ctp
<div> <?php echo $form->create('User', array( 'url' => array('controller' => 'users', 'action' => 'index'), )); ?> <fieldset> <legend>抽出条件</legend> <?php $types[99] = 'すべて'; echo $form->input('type', array('label' => 'タイプ', 'options' => $types)); echo $form->end('抽出'); ?> </fieldset> </form> </div> <?php // paginatorに検索条件を渡す $paginator->options(array('url' => $this->passedArgs)); ?> 表示テーブル <?php echo $paginator->first('<<最初'); echo $paginator->prev('<前'); echo $paginator->numbers(); echo $paginator->next('次>'); echo $paginator->last('最後>>'); ?>
そうすると
/users/index/page:2/type:1
のようなURLになる