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'
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.