我有一個項目的問題,我需要複製一些行,但不是在數據庫中,我需要做之後做一個選擇查詢,我試圖做一個代碼在該方法找到了模型後,但我認爲分頁確實工作後,發現我是對的?所以當我嘗試修改包含我的結果,訂單丟失的陣列和永遠不會再工作......我的代碼是這樣的......CakePHP afterFind和分頁順序不起作用
public function afterFind($results) {
foreach ($results as $rKey = $result) {
if (count ($result['Address']) 1) {
foreach ($result['Address'] as $address) {
$res[] = array('Client' => $result['Client'],
'Address' => $address
);
unset($results[$rKey]);
}
}
}
return $res;
}
所以,這是給有分頁的問題.. 。有人能幫我嗎??謝謝...
修訂
在模型:
公共$爲了=陣列( 'Cliente.is_anunciante'=> 'DESC', 'Cliente.nome_fantasia'=>' ASC');
public function paginate(
$conditions, $fields, $order, $limit, $page = 1,
$recursive = null, $extra = array()
) {
$results = $this->getResults($conditions, $fields, $order, $limit, $page, $recursive, $extra);
return $results;
}
public function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
$results = $this->getResults($conditions, $recursive, $extra, null, null, null, null);
return count($results);
}
public function getResults($conditions, $fields, $order, $limit, $page, $recursive, $extra) {
if(!empty($page))
$results = $this->find('all', compact('conditions', 'fields', 'order', 'limit', 'page', 'recursive', 'group'));
if(empty($page))
$results = $this->find('all', compact('conditions', 'recursive', 'extra'));
foreach ($results as $rKey => $result) {
if(!empty($result['Endereco']) && count($result['Endereco']) > 1 && $result['Cliente']['is_anunciante'] != 2){
foreach ($result['Endereco'] as $key => $endereco) {
$results[] = array(
'Cliente' => $result['Cliente'],
'Endereco' => array(
$endereco
)
);
}
unset($results[$rKey]);
}
}
return $results;
}
在控制器的動作裏面:
$this->paginate = array(
'limit' => 20,
'order' => array(
'is_anunciante' => 'DESC',
'nome_fantasia' => 'ASC'
)
);
它的原因是paginateCount方法不知道你要添加多少行,使用patryate和paginateCount方法覆蓋它們。 – 2014-09-11 03:03:11
如果頁面爲空,那麼'$ limit'是什麼? – 2014-09-18 03:07:18