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.

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

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码