我现在已经在这几个小时了。阅读大约7或8个在线教程,我仍然无法使用我的搜索结果进行分页。
以下是这种情况的基础知识:
在我的plans_controller中,我在我的搜索查询功能中调用它
$this->set('plans', $this->Plan->find('all', $options));
通过执行
,我最接近(没有抛出SQL错误)$this->set('plans', $this->Plan->find('all', $options), $this->paginate());
注意: $ options是实际的搜索查询连接和条件(并且查询可以完美地执行分页)。
但是上面没有做任何事情来将搜索查询带到以下页面,甚至没有在搜索的第一个结果集上工作。
使用分页来满足我的需求似乎与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'));
}
答案 0 :(得分: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'));