2012-04-18 9 views
0

検索結果を改ページできません。私のセットアップは以下の通りです。CakePHP:モデル内の検索ロジックで改ページする

myapp.com/searches/productsに検索フォーム(検索フォームあり).../app/views/searches/products.ctpがあります。私はProductモデルをこの検索クエリに使用するSearchesコントローラを採用しています。 Productには、検索ロジック($this->find(...))のアクションsearch()があります。検索結果は、ビューのフォームの下に表示されます。

コントローラで通常行われる$this->paginate()に似たような作業を行うにはどうすればよいですか?さらに、私は設定に何か問題があるかどうか、特にフォームと検索結果の両方を含むビューであるかどうかを知りたいと思います。

答えて

3

モデルでの検索ロジックを維持し、まだコントローラにページ付けする1つの方法は、これを行うことです。

説明:

代わりのモデルから実際の結果を返し、任意の/すべてを返すオプションを探して、通常のように改ページします。いくつかの例では、以下のように単純すぎるように見えますが、find()にはさらに多くのオプションを追加する余地があります。containordergroupjoinsconditions ...などなどです。 「Fat Models、Skinny Controllers」のマントラを使用しています。

find()にこのようなオプションを設定するといいです。サイト全体で簡単に再利用できるようになります。さまざまなオプションを渡すだけでよいのです。

コード:

/* CONTROLLER 
*/ 
$opts = array('paginate' => true, 'limit'=>20); 
$paginateOptions = $this->Event->getEvents($opts); 
$this->paginate = $paginateOptions; 
$data = $this->paginate('Event'); 

/* MODEL 
*/ 
public function getProducts($opts = null) { 

    $params = array(); 

    //limit 
    $params['limit'] = 50; //default 
    if(!empty($opts['limit'])) $params['limit'] = $opts['limit']; 

    //paginate option 
    $paginate = false; 
    if(isset($opts['paginate'])) { 
     if($opts['paginate']) $paginate = true; 
    } 

    //either return the options just created (paginate) 
    if($paginate) { 
     return $qOpts; 

    //or return the events data 
    } else { 
     $data = $this->find('all', $qOpts); 
     return $data; 
    } 
} 

あり、コードのこのビットスリム/以下の行を記述するための方法である - しかし、私はこのようにそれを書くようなので、それはすぐに理解できるのです。

(あなたの全体的な構造に何か問題があるように表示されません。)

0

私は通常のセッションで検索パラメータを格納し、コントローラのアクション内のすべてを処理するために使用します。

function indexbystatus() { 
    $this->set('title_for_layout','List Assets by Status'); 
    $this->Session->write('sender',array('controller'=>'assets','action'=>'indexbystatus')); 
    $searchkey=$this->Session->read('Searchkey.status'); 
    $conditions=''; 
    if($searchkey) { 
     $conditions=array('Asset.status_id'=>$searchkey); 
    } 
    if(!empty($this->data)) { 
     // if user has sent anything by the searchform set conditions and 
     // store it to the session but if it is empty we delete the stored 
     // searchkey (this way we can reset the search) 
     if($this->data['Asset']['status_id']!='') { 
      $conditions=array('Asset.status_id'=>$this->data['Asset']['status_id']); 
      $this->Session->write('Searchkey.status',$this->data['Asset']['status_id']); 
     } else { 
      $this->Session->delete('Searchkey.status'); 
      $conditions=null; 
     } 
    } else if($searchkey) { 
     // if no data from the searchform we set the stored one 
     // from the session if any 
     $this->data['Asset']['status_id']=$searchkey; 
    } 
    $this->paginate=array(
         'limit'=>25, 
         'order'=>array('Asset.status_id'=>'asc'), 
         'conditions'=>$conditions, 
         ); 
    $this->set('assets',$this->paginate()); 
    $statuses=$this->Asset->Status->find('list'); 
    $this->set('statuses',$statuses); 
} 

私はちょうど私が行動することによって、さまざまなソリューションとロジックを持つことができるので、それはより多くのモデルではないコントローラのアクションでそれを処理するのが好きです。

関連する問題