2011-01-31 56 views
1

我一直在黑客這個幾個小時了。閱讀大約7或8個在線教程,我仍然無法使用我的分頁處理搜索結果。CakePHP分頁與find()和一個複雜的查詢

這裏是形勢的基本知識:

在我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變量包含的查詢參數正常的陣列,所有你需要做的是更換的正常find呼叫與電話paginate

$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 2011-01-31 06:25:11