Cakephp afterFind和分页顺序不工作 [英] Cakephp afterFind and pagination order not working
问题描述
我有一个项目的问题,我需要重复一些行,但不是在数据库中,我需要做后做一个select查询,我试图在方法afterFind的模型中做一个代码,但我认为分页做工作后发现我是对的吗?所以当我尝试修改包含我的结果的数组,顺序会丢失,从来不工作...我的代码是这样的...
i am having a problem with a project, i need to duplicate some rows but not in the database, i need to do it after to do a select query, i tried to make a code in the method afterFind of the model, but i think the pagination do works after find am i right? so when i try to modify the array that contains my result the order is lost and never works again...my code is something like this...
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;
}
所以,这是给分页的问题...有人可以帮我??感谢...
So, this is giving a problem with pagination...can someone help me?? Thanks...
UPDATED
In the model:
public $ order = array('Cliente.is_anunciante'=>'DESC','Cliente.nome_fantasia'=>'ASC');
public $order = array('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'
)
);
推荐答案
尝试这样做 -
public function paginate(
$conditions, $fields, $order, $limit, $page = 1,
$recursive = null, $extra = array()
) {
return $this->getResults($conditions, $limit, $page, $order);
}
getResults
public function paginateCount($conditions = null, $recursive = 0, $extra = array())
{
$results = $this->getResults($conditions, $limit, $page, $order);
return count($results);
}
这篇关于Cakephp afterFind和分页顺序不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!