Controller 的模型查询函数 :: 如何在 Controller 中调用分页功能?
我无法解决如何将模型查询函数调用到控制器中的问题。我已经查阅了太多的文档。也许我在尝试做的事情上完全错了?继续得到MySQL错误(错误如下)。
Plan :: Model:
function getActive()
{
$findParameters = array(
'limit' => 10,
'order' => array('Plan.monthly_cost' => 'asc'),
'conditions' => array('PlanDetail.active' => 1)
);
return $this->find('all', $findParameters);
}
Plan :: 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 子句”中的未知列“计划”
I cannot resolve how to call a model query function into a controller. I've been through too much documentation to count. Maybe I am wrong in what I am trying to do altogether? Continue to get MySQL errors (errors below).
Plan :: Model:
function getActive()
{
$findParameters = array(
'limit' => 10,
'order' => array('Plan.monthly_cost' => 'asc'),
'conditions' => array('PlanDetail.active' => 1)
);
return $this->find('all', $findParameters);
}
Plan :: 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));
}
Notice (8): Array to string conversion [ROOT....
Warning (512): SQL Error: 1054: Unknown column 'Plan' in 'where clause'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上 $this->paginate 不接受查询结果作为第一个参数。如果您非常喜欢在模型中包含数据库逻辑,请这样做:
模型
在您的控制器中:
说明: paginate 方法对作为第一个参数传递的模型进行分页(或者如果它是 null 则获取控制器的默认模型)并使用第二个参数对结果应用一些限制。
检查此 recursive=2 的 Containable 行为,或者至少取消绑定这些不必要的关系。
Basically $this->paginate doesn't accept result of the query as first parameter. If you so much like to have your DB logic in the model do it this way:
Model
in your Controller:
The explanation: paginate method paginates a model passed as first argument (or if it's null get's the controller's default model) and uses second parameter to apply some restrictions for the result.
Check Containable behaviour for this recursive=2, or at least unbind these relations which are not necessary.