开发者

Model query function for Controller :: how do I call w/ pagination in Controller?

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. ***//
    $ac开发者_如何学运维tive = $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'


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜