2012-05-03 6 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) 확인 유효한. 나는 이것이 100 % 올바른 방법이 아니라고 확신한다.
  2. 성함을 비워두면 아래 URL과 같이 작성되며 정확하지 않거나 보이지 않는 이름, 성 등 2 개의 자유 양식 필드가 있습니다. 즉, 아래 항목이 발생하지 않도록 기본값을 지정해야하며 매우 동적이지는 않습니다. 새로 고침에

    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 할 수 있습니다.

더 많은 것 또는 더 적게 원하는 것을 자동화하고 이미 코드보다 더 많은 작업을 수행 할 수 있습니다.

그래서 직접 플러그인을 사용하거나 트릭을 어떻게하는지 살펴 보시기 바랍니다. :)

관련 문제