2012-05-03 7 views
2

私はcakephpアプリケーションの結果ページに「検索」ボックスを作成しようとしています。このページでは、cakePHPページ分割コンポーネントを使用して、結果を表示および「ページング」します。これは完璧ですが、私は次の部分を働かせるのが難しいです。cakePHPページングとpassedArgs

所望の結果:

  1. CakePHPのフォーム(ポスト)入力ボックス、日付選択を含む選択ボックスのカップルと私は日付の間で選択することができるように。ユーザーはこれらのフィールドにデータを入力して送信することができます
  2. 送信時に、コントローラのcakePHPページング条件を変更する必要があります。
  3. ビューでは、ユーザー選択の記録を保持するようにします。別のページをフィルタリングすると、ユーザーの検索が維持されます。私はこれが$ this-> passedArgsを使って達成できると理解しています。なぜ私が投稿を使用していて、取得しないのですか?

コード:私は今の私

// Form: 
<?php 
     echo $this->Form->create('search', array('class' => false)); 
      echo $this->Form->input('searchFor'); 
      echo $this->Form->input('dateFrom'); 
      echo $this->Form->input('dateTo'); 
     echo $this->Form->end(); 
?> 

// Controller: 
if($this->request->is("post")) { 
     $filters = $this->request->data["search"]; 
     $this->passedArgs["searchFor"] = $filters["searchFor"]; 
     $this->passedArgs["dateFrom"] = $filters["dateFrom"]." 00:00:00"; 
     $this->passedArgs["dateTo"] = $filters["dateTo"]." 00:00:00"; 


     // Assign search parameters: 
     if($this->passedArgs["searchFor"] != "") { 
      $conditions["Model.field LIKE"] = "%".$this->passedArgs["searchFor"]."%"; 
     } 

     $conditions["Model.created >="] = $this->passedArgs["dateFrom"]; 
     $conditions["Model.created <="] = $this->passedArgs["dateTo"]; 


} else { 
     $conditions = array("Result.status_id >=" => 12); 
} 

$this->paginate = array(
    'conditions' => $conditions, 
    'order' => array('Result.created ASC'), 
    'limit' => 20 
); 

$this->set("results",$this->paginate("Model"); 


// The view file: 
<?php 
     $this->Paginator->options(array('url' => $this->passedArgs)); 
?> 

  1. 結果
  2. のすべてと初期ページがロード私は、検索ボックスにそれを移入私の結果を返します

問題:

  1. 結果を掲載してpassedArgsがある場合b)にチェックされていた場合、私は、私は今、2つのチェックを行う必要があるとして、私はそれをやっている方法が間違っている確信しa)はしています利用可能です。私はこれが正しいことではないと100%確信しています。
  2. 姓を空白にしておくと、私のURLは以下のように書かれ、これは見た目にも正しくないようにも見えます。つまり、以下の項目が発生しないようにデフォルト値を割り当てる必要があります。これはあまり動的ではないようです。リフレッシュに

    http://localhost/site/controller/action/surname:0/name:John/date:0/

  3. それが掲示値が利用できanylongerないため、ページが存在しないと言います。

答えて

6

通常、私は、コントローラで次のように進む:

//transform POST into GET 
if($this->request->is("post")) { 
    $url = array('action'=>'index'); 
    $filters = array(); 

    if(isset($this->data['searchFor']) && $this->data['searchFor']){ 
     //maybe clean up user input here??? or urlencode?? 
     $filters['searchFor'] = $this->data['searchFor']; 
    } 
    //redirect user to the index page including the selected filters 
    $this->redirect(array_merge($url,$filters)); 
} 

$conditions = array(); 
//check filters on passedArgs 
if(isset($this->passedArgs["searchFor"])){ 
    $conditions["Model.field LIKE"] = "%".$this->passedArgs["searchFor"]."%"; 
} 

//paginate as normal 
$this->paginate = array(
    'conditions' => $conditions, 
    'order' => array('Result.created ASC'), 
    'limit' => 20 
); 

アイデアはGETにあなたのフォームから送信されたPOSTを変換することです。あなたは文句を言わないページネータとの問題もリフレッシュ

希望を持って、これは、

+0

優秀な答えを助けありがとう – mauzilla

1

このsearch pluginを使用すると、もっと簡単にDRYできます。

多かれ少なかれあなたのコードより多くのことを行うことができます。

ですから、プラグインを直接使用するか、トリックをどのように行うかを見てみましょう。 :)

関連する問題