Controller 的模型查询函数 :: 如何在 Controller 中调用分页功能?

发布于 2024-10-13 10:44:53 字数 775 浏览 1 评论 0原文

我无法解决如何将模型查询函数调用到控制器中的问题。我已经查阅了太多的文档。也许我在尝试做的事情上完全错了?继续得到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不是你的备胎 2024-10-20 10:44:53

基本上 $this->paginate 不接受查询结果作为第一个参数。如果您非常喜欢在模型中包含数据库逻辑,请这样做:

模型

function getActiveConditions() {
    retrun array(
       'limit' => 10,
       'order' => array('Plan.monthly_cost' => 'asc'),
       'conditions' => array('PlanDetail.active' => 1)
    );
}

在您的控制器中:

function search() {
    $this->Plan->recursive = 2;
    $activeConditions = $this->Plan->getActiveConditions();
    $this->set('plans', $this->paginate($this->Plan, $activeConditions));
}

说明: 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

function getActiveConditions() {
    retrun array(
       'limit' => 10,
       'order' => array('Plan.monthly_cost' => 'asc'),
       'conditions' => array('PlanDetail.active' => 1)
    );
}

in your Controller:

function search() {
    $this->Plan->recursive = 2;
    $activeConditions = $this->Plan->getActiveConditions();
    $this->set('plans', $this->paginate($this->Plan, $activeConditions));
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文