duangai1368 2011-01-21 17:59
浏览 69
已采纳

Controller的模型查询功能::如何在Controller中调用w / pagination?

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'

  • 写回答

1条回答 默认 最新

  • douyi7283 2011-01-21 22:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算