5

ユーザー モデルを実装し、性別、年齢、場所で検索するための単純な検索フォームを作成しました。ページネーターを使用して結果をページ付けしています。問題は、最初のページの検索条件しか覚えていないことです。2ページ目、または次へなどをクリックすると、デフォルトですべてのユーザーの検索に戻ります。

これは私のコントローラーの汚い検索コードです。基本的には、送信されたフォーム フィールドをチェックし、Users テーブルの一致するフィールドに対してクエリを実行し、結果をページ付けします。

if (!empty($this->data)) { 
    // by name 
    if (!empty($this->data['User']['search_name'])) { 
  $this->paginate = array('conditions' => array('visible'=>1, 
'OR'=>array( 
                'User.username LIKE' => '%'.$this->data['User']['search_name'].'%', 
                'User.firstname LIKE' => '%'.$this->data['User']['search_name'], 
                'User.lastname LIKE' => '%'.$this->data['User']['search_name']) 
                 ), 'limit'=>'10', 'order'=>'User.username'); 
   } 
   // by gender 
   else if (!empty($this->data['User']['search_gender'])) { 
  $this->paginate = array('conditions' => array( 
  'visible'=>1, 
                'User.gender' => $this->data['User']['search_gender'] 
                ), 'limit'=>'10', 'order'=>'User.username'); 
  } 
  // by state 
  else if (!empty($this->data['User']['search_state'])) { 
  $this->paginate = array('conditions' => array( 
  'visible'=>1, 
                'User.state' => $this->data['User']['search_state'] 
                ), 'limit'=>'10', 'order'=>'User.username'); 
  } 

   // Send the results for the above criteria to the view 
   $results = $this->paginate('User'); 
   $this->set('users', $results); 

     } 
     // Default retrieval of all users 
    else { 
        $this->paginate = array('conditions'=>array('visible'=>1), 
'limit'=>'10', 'order'=>'User.username'); 
    $this->set('users', $this->paginate('User')); 
  } 

ページネーションの後続のページに検索条件を記憶させる方法を見つけようとしています。助けてくれてありがとう。

4

1 に答える 1

8

$this->data検索フォームを投稿することで入力されるため、2 ページ目は空です (2 ページ目には投稿されません)。

検索フォームの投稿を GET () に変更し、代わりに$this->Form->create('ModelName', array('type'=>'get')解析します$this->params$this->data

于 2012-11-06T17:04:11.873 に答える