php - Controller 的模型查询函数::如何在 Controller 中调用 w/pagination?

标签 php mysql cakephp mysql-error-1054

我无法解决如何将模型查询函数调用到 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/

相关文章:

php - 如何比较从数据库中检索的两个数据数组?

php - 用 PHP 发送带有附件的电子邮件?

php - PHP 和 MySQL 的新帖子通知

php - CakePHP 管理面板

javascript - 使用 CakePHP 的文本输入创建弹出窗口

json - CakePHP 3 : Exception handling/serialization in a RESTful API

php - 从 php 连接到 mysql 数据库,出错但没有显示错误?

PHP 多数据库表搜索

java - 在 hibernate 中读写通用枚举

MySQL 导出到 outfile : CSV escaping chars