我无法解决如何将模型查询函数调用到 Controller 中。我已经阅读了太多无法计数的文档。也许我在尝试做的事情上完全错了?继续获取 MySQL 错误(错误如下)。
计划::模型:
function getActive()
{
$findParameters = array(
'limit' => 10,
'order' => array('Plan.monthly_cost' => 'asc'),
'conditions' => array('PlanDetail.active' => 1)
);
return $this->find('all', $findParameters);
}
计划:: Controller :
function search() {
$this->Plan->recursive = 2; //*** Modified by Jason: Recursion needs to be corrected with better method. ***//
$active = $this->Plan->getActive();
$this->set('plans', $this->paginate($active));
}
注意事项(8):数组到字符串的转换[ROOT.... 警告 (512):SQL 错误:1054:“where 子句”中的未知列“计划”
最佳答案
基本上 $this->paginate 不接受查询结果作为第一个参数。如果您非常喜欢在模型中使用您的数据库逻辑,请这样做:
型号
function getActiveConditions() {
retrun array(
'limit' => 10,
'order' => array('Plan.monthly_cost' => 'asc'),
'conditions' => array('PlanDetail.active' => 1)
);
}
在您的 Controller 中:
function search() {
$this->Plan->recursive = 2;
$activeConditions = $this->Plan->getActiveConditions();
$this->set('plans', $this->paginate($this->Plan, $activeConditions));
}
解释:paginate 方法对作为第一个参数传递的模型进行分页(或者如果它为 null,则获取 Controller 的默认模型)并使用第二个参数对结果应用一些限制。
检查此 recursive=2 的 Containable 行为,或者至少解除绑定(bind)这些不必要的关系。
关于php - Controller 的模型查询函数::如何在 Controller 中调用 w/pagination?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4762141/