2011-01-31 4 views
1

나는 지금 몇 시간 동안 해킹을 해왔다. 약 7 또는 8 개의 온라인 자습서를 읽고 여전히 내 검색 결과로 작업하는 페이지 매김을 얻을 수 없습니다. 여기 find()와 복잡한 쿼리를 이용한 CakePHP 페이지 매김

이 상황의 기본입니다 : 내 plans_controller에서

, 내 검색 쿼리 기능이 호출입니다

 $this->set('plans', $this->Plan->find('all', $options)); 

내가 수행하여 (어떤이 SQL 오류를 던지고되지 않은) 온 가장 가까운

$this->set('plans', $this->Plan->find('all', $options), $this->paginate()); 

참고 : $ 옵션은 실제 검색 쿼리 조인 및 조건 (그리고 쿼리/w를 완벽하게 작동 out 매김).

위의 내용은 검색 쿼리를 다음 페이지로 전달하지 않았으며 검색에서 설정된 첫 번째 결과에도 적용되지 않았습니다.

내 요구 사항에 페이지 매김을 사용하는 것은 CakePHP와 매우 복잡해 보이지만,이 스레드를 사용하여 나는이 (그리고 다른 간단하면서도 효과적인 조언을 환영합니다 :))을 얻기 시작해야하는 좀 더 둥근 설명을 원합니다.

이 형식의 복잡한 페이지 매김을 시도한 것은 이번이 처음입니다.

다음은 내 컨트롤러 코드입니다. 당신은 당신이 할 필요가 교체입니다,하지만 여전히이

var $name = 'Plans'; 

function beforeFilter() { 
    $this->Auth->allow('search','index'); } 

function search() { 

    $this->Plan->recursive = 2; 

    if(isset($this->data['Plan']['ApplicantAge'])) { 
     $ApplicantAge = $this->data['Plan']['ApplicantAge']; 
    } else { 
     $ApplicantAge = 25; 
    } 
    if(isset($this->data['Plan']['SpouseAge'])) { 
     $SpouseAge = $this->data['Plan']['SpouseAge']; 
    } else { 
     $SpouseAge = 0; 
    } 
    if(isset($this->data['Plan']['NumberChildren'])) { 
     $NumberChildren = $this->data['Plan']['NumberChildren']; 
    } else { 
     $NumberChildren = 0; 
    } 
    if(isset($this->data['Plan']['Vision'])) { 
     $Vision = $this->data['Plan']['Vision']; 
    } else { 
     $Vision = 0; 
    } 
    if(isset($this->data['Plan']['ZipCode'])) { 
     $Zip = $this->data['Plan']['ZipCode']; 
    } else { 
     $Zip = 0; 
    } 

    $memberCount = 1; //We can assume the applicant is there 
    if($SpouseAge > 0) { 
     $memberCount += 1; 
    } 
    if($NumberChildren > 0) { 
     $memberCount += $NumberChildren; 
    } 

    //2: Combo plan (1 adult + children, 1 adult + spouse + children) 
    $comboType = 'sa'; 
    if($ApplicantAge < 18) { 

     //$comboType = 'sc'; 
    } 
    if($SpouseAge > 0) { 
     if($NumberChildren > 0) { 
      $comboType = 'asc'; 
     } else { 
      $comboType = 'as'; 
     } 
    } else { 
     if($NumberChildren > 0) { 
      $comboType = 'ac'; 
     } 
    } 


     $options = array(
     'joins' => array (
      array( 
       'table' => 'plans_zips', 
       'alias' => 'PZips', 
       'type' => 'inner', 
       'foreignKey' => false, 
       'conditions'=> array('Plan.id = PZips.plan_id') 
      ), 
      array( 
       'table' => 'zips', 
       'alias' => 'Zips', 
       'type' => 'inner', 
       'foreignKey' => false, 
       'conditions'=> array('Zips.id = PZips.zip_id') 
      ) 
     ), 
     'conditions' => array(
      "AND" => array(
       array($ApplicantAge . ' BETWEEN Age.Min_Age AND Age.Max_age'), 
       'Zips.title' => $Zip, 
       'Applicant.amount' => array($comboType, $memberCount), 
       'PlanDetail.active' => 1) 
     ) 
    ); 

    $queryStr = "SELECT Plan.* FROM plans AS Plan "; 
    $queryStr = $queryStr . "INNER JOIN ages on age_id = ages.id "; 
    $queryStr = $queryStr . "INNER JOIN applicants on applicant_id = applicants.id "; 
    $queryStr = $queryStr . "WHERE (applicants.amount = '". $memberCount . "' OR applicants.amount = '" . $comboType . "')"; 
    $queryStr = $queryStr . " AND (". $ApplicantAge . " BETWEEN ages.Min_Age+0 AND ages.Max_Age+0) "; 
    $queryStr = $queryStr . " AND Plan.id IN (SELECT plan_id FROM plans_zips where zip_id = (SELECT id FROM zips WHERE title = '". $Zip. "'))"; 
    //Add the vision limiting item 
    if($Vision == 1) { 
     $queryStr = $queryStr . " AND dental_cost > 0"; 
     array_push($options['conditions'], "dental_cost > 0"); 
    } 

    //$this->set('plans', $this->Plan->find('all', $options)); 

    $this->paginate = $options; 
    $plans = $this->paginate(); 
    $this->set(compact('plans')); 
} 

답변

4

당신의 $options 변수가 쿼리 매개 변수의 정상적인 배열을 포함 가정 .: 해결하기 위해 적용 할 노력하고 그것의 그 정도를 볼 수 있습니다 paginate 전화와 일반 find 전화 :

$options = array('conditions' => ...); 

// normal find call 
// $plans = $this->Plan->find('all', $options); 

// same thing with pagination: 
$this->paginate = $options; 
$plans = $this->paginate(); 

$this->set(compact('plans')); 
+0

안녕 Deceze, 나는 (위 게시) 내 쿼리 "정상적인 배열을"고려할 것입니다 있는지 확실하지 않습니다. 나는 정상이 아니라고 말할 것이다.). 나는 당신의 제안을 시도했으나 제대로 작동하지 않습니다. 나는 이것을보고 생각하기 시작했다. 1) url을 통해 매개 변수를 전달하는 것이 가장 좋을 것이다. 그리고 2) url에 검색 매개 변수를 유지하여 페이지가 페이지 매김 될 때 검색이 페이지를 통해 유지되도록한다. 하지만 페이지가 페이지 매김 될 때 결과가 새로운 것인지 확인해야합니다. 다음 >> >> 예를 들어 이것은 매우 까다 롭습니다! – OldWest

+0

@Old 원래 코드에서 나를 놀라게 한 것은'$ this-> paginate()'를 잘못 사용했다는 것입니다. 나는 현재 코드가 작동해야하는지 여부에 대해서는 정말로 언급 할 수 없다. 특정 합병증이 무엇인지 명확히 할 수 있습니까? – deceze

+0

나는이 사실을 더 혼란스럽게 만들고있다. 기본적으로, 나는 이것이 작동하도록하기 위해 어떻게 접근해야할지 모르겠다. 페이지 당 5 개의 레코드로 제한된 검색 결과에 페이지 매김을 할 수 있어야하고 페이지가 변경 될 때 쿼리를 유지해야합니다 (이전 또는 다음). 검색 데이터는 각 페이지마다 고유해야합니다. 즉, 중복 결과가 없습니다. 이것이 내가 달성하려고하는 것이지만, 어디서부터 시작해야하는지, 아니면 최선의 접근 방식이 무엇인지는 알지 못합니다. 추가 조언 해 주셔서 감사합니다.) – OldWest

관련 문제