PR

[CakePHP] あいまい検索とpaginatのURLパラメータとか

前回、検索を簡単にできるけれどあいまい検索ができないという結果になった。

pc.casey.jp » CakePHPの検索機能 :
https://pc.casey.jp/archives/2289

単純に検索するだけなら簡単に実装できるが、検索結果を paginate で sort させると検索文字が消えてしまう。これは paginate が生成する URL に検索文字が含まれないためだ。 paginate に値を渡す方法はいくつも掲載されていたがどういうわけかうまく行かなかった。そこで、改めて避けては通れないだろう検索機能について調べてみた。いろいろな実装方法があるだろうし、美しいとは相変わらず言えないが参考程度に。


スポンサードリンク

controller

  $q = isset($this->params['url']['Keyword'])? $this->params['url']['Keyword'] : null;
 // $q = isset($this->data['Item']['Keyword']) ? $this->data['Item']['Keyword'] : null;
  $this->layout = 'item_search';
  $this->Item->recursive = 0;
  $this->set('q', $q);
  $this->set('items', $this->paginate(array(
   'Item.group_id ='=> $this->Auth->user('group_id'),
   'Item.name LIKE' => '%' . $q . '%',
  )));

view

 <tr>
   <th><?php echo $this->Paginator->sort('id',           null,array('url'=>array('?'=>array('Keyword'=>$q))));?></th>
   <th><?php echo $this->Paginator->sort('name',         null,array('url'=>array('?'=>array('Keyword'=>$q))));?></th>
   <th><?php echo $this->Paginator->sort('item_group_id',null,array('url'=>array('?'=>array('Keyword'=>$q))));?></th>
   <th><?php __('Actions');?></th>
 </tr>

※CakePHP 1.3.2

コメント