對於CakePhp 2.5對模型功能分頁結果
我在我的控制器中有以下搜索功能。
public function search($query = null, $lim = null)
{
$tokens = explode(" ", $query);
$this->Paginator->settings = array(
'conditions' => array(
'Customer.site_id LIKE' => '%' . $this->viewVars['shopId'],
'CONCAT(Customer.first_name," ",Customer.last_name," ",Customer.organisation) LIKE' => '%' . implode(' ', $tokens) . '%'
),
'limit' => $lim
);
$this->set('customers', $this->Paginator->paginate());
}
這工作正常,並得到我想要的結果。
然而,它已經向我建議,我應該把這些搜索功能在我的模型。我可以很容易地做到這一點,並已經這樣做了類似在我的模型如下:
public function getActiveTasks($id){
return $this->Task->find('all', array(
'conditions' => array(
'Task.customer_id' => $id,
'Task.status' => 0
),
'order' => array('Task.due_date ASC'),
'recursive' => -1,
));
}
我遇到的問題是,我不能(或不知道如何)使用分頁程序自定義搜索。有沒有辦法對自定義函數的結果進行分頁?
即我可以做到以下幾點:
public function CustomModelSearch($query = null, $lim = null)
{
return $this->Task->find('all', array(
'conditions' => array(
'Customer.site_id LIKE' => '%' . $this->viewVars['shopId'],
'CONCAT(Customer.first_name," ",Customer.last_name," ",Customer.organisation) LIKE' => '%' . implode(' ', $tokens) . '%'
),
'limit' => $lim
));
}
,然後在控制器調用
$results = $this->Model->CustomModelSearch($query, $lim);
$this->set('results',$this->Paginate($results));
我不能找到一種方法,結果傳遞到分頁程序來渲染分頁結果的看法,除非我通過控制器按第一塊的代碼,這是我一直在說使用它是不好的MVC原則。
我意識到這一點。發佈的功能只是一個例子來說明我的觀點。 – 2014-10-30 22:21:48
把你的問題說明你的問題和錯誤給您和您的代碼wihtout例子 – CoolLife 2014-10-31 01:13:42