CakePHPにページネーションを実装しようと思いましたが、2ページ目以降のリンクを押すと検索条件が保存されず上手く機能しません。
これは有名な不具合らしく、主な解決策は、セッションを用いて検索条件を保存する方法があるということなのですが。
今回は以下のサイトを参考にさせていただいています。
参考サイト:初めての[CakePHP2.x]での開発、そして挫折するまで日記~その27 検索結果をページネーションで表示させる
しかし、なぜかタイトルにある通り、セッションに書き込めない、もしくは読み込めない状態で解決策がわかりません。
ちなみに別のページ(indexなど)でSessionComponent::writeやSessionComponent::readを試したところ問題なく動きました。
なので、セッションの設定などに間違いがあるということでもなさそうです。
以下に問題のあるコードを記載します。いったいどこが間違っているのでしょうか?
具体的な解決策をわかりやすくお願いします。
追記1:var_dumpで確認したところ、$this->Session->write('conditions', $conditions);がfalseになっていました。なぜ、falseになってしまうのかがわかりません。
追記2:if($this->request->is('post') && $this->request->data != ""){}内の一行目に$this->Session->writeを使うと、値でも配列でも変数でも書き込めてvar_dumpでtrueとなりました。
$areas_id = $this->request->data['area'];以降にセッションを書き込むとどのような値でもfalseとなってしまいます。
ただの変数代入の行の前後でなぜ、このようなことが起きてしまうのでしょうか?
引き続き、よろしくお願いします。
PHP
1<?php 2// app/Controller/SitesController.php 3App::uses('AppController', 'Controller'); 4 5class SitesController extends AppController { 6 7 public $uses = array('Spot', 'Area', 'Category', 'SpotsStatus', 'SubCategory', 'Plan', 'PlansSpot'); 8 public $components = array('Paginator', 'Flash', 'Session'); 9 10 public $paginate = array( 11 'Spot' => array( //モデルごとにページネーションの設定を記入 12 'limit' =>10, //1ページ表示できるデータ数の設定 13 'order' => array('id' => 'asc'), //データを降順に並べる 14 ) 15 ); 16 17 public function search($page = null, $sort = null, $direction = null) { 18 $this->Paginator->settings = $this->paginate; 19 $this->Spot->recursive = 0; 20 $this->set('area', $this->Area->find('list', array('fields' => array('id', 'name')))); 21 if($this->request->is('post') && $this->request->data != ""){ 22 //Formの値を取得 23 $areas_id = $this->request->data['area']; 24 //session用 25 $search['areas_id'] = $areas_id; 26 $conditions['areas_id'] = $areas_id; 27 //検索条件をセッションに保存 28 if($this->Session->check('conditions')){ 29 $this->Session->delete('conditions'); //古いのは削除 30 }; 31 if($this->Session->check('search')){ 32 $this->Session->delete('search'); //古いのは削除 33 }; 34 $this->Session->write('conditions', $conditions); 35 $this->Session->write('search', $search); 36 37 $this->Paginator->settings['Spot']['conditions'] = array( 38 'areas_id' => $conditions['areas_id'], 39 ); 40 $this->set('results', $this->Paginator->paginate('Spot')); 41 $this->set('data', $this->Session->read('conditions')); 42 }else{ 43 if($this->Session->check('conditions')){ 44 //パラメータがなければ新しい検索のため削除 45 if(empty($this->params['named']['page']) && empty($this->params['named']['sort']) && empty($this->params['named']['direction'])){ 46 $this->Session->delete('conditions'); 47 $this->Session->delete('search'); 48 }else{ 49 $conditions = $this->Session->read('conditions'); 50 $search = $this->Session->read('search'); 51 $this->Paginator->settings['Spot']['conditions'] = array( 52 'areas_id' => $conditions['areas_id'], 53 ); 54 $this->set('results', $this->Paginator->paginate('Spot')); 55 $this->set('search', $search); 56 } 57 } 58 }; 59}
PHP
1// app/View/Sites/search.ctp 2 <?php echo $this->Form->create('Sites', array('type'=>'post','action'=>'./search')); 3 echo $this->form->input('area' ,array( 4 'type'=>'select' , 5 'multiple' =>'checkbox', 6 'options' => $area, 7 //'selected' => $selected, 8 'label' => false, //ここのラベルは'area'の部分をこのチェックボックスのタイトルにしないためのもの 9 'hiddenField' => false, 10 'div' => false 11 ) 12 ); 13 echo $this->Form->end('Finish'); ?> 14 15 <?php foreach ($results as $result): ?> 16 <?php 17 echo $this->Html->link( 18 $result['Spot']['name'], 19 array('controller' => 'sites', 'action' => 'view', $result['Spot']['id']) 20 ); 21 ?> 22 <?php echo $result['Areas']['name']; ?> 23 <?php endforeach; ?> 24 25 <div class="paging"> 26 <?php 27 echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled')); 28 echo $this->Paginator->numbers(array('separator' => '')); 29 echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled')); 30 ?> 31 </div> 32 <?php 33 echo $this->Html->link( 34 'トップ', 35 array('controller' => 'sites', 'action' => 'index') 36 ); 37 ?> 38 <?php debug($data); //確認用 使わなくなれば消してよい ?>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。