2014-09-01 122 views
0

我需要根據afterFind回調中創建的字段($results[$key]['Movimento']['status'])設置我的註冊分頁順序。Cakephp分頁自定義訂單

一個afterFind:

public function afterFind($results, $primary = false) { 

    foreach ($results as $key => $val) { 
     if(isset($val['Movimento']['data_vencimento'])) { 
      $data = $val['Movimento']['data_vencimento']; 
      $dtVc = strtotime($data); 
      $dtHj = strtotime(date('Y-m-d')); 
      $dtVencendo = strtotime("+7 day", $dtHj); 
      if ($dtVc < $dtHj) { 
       $results[$key]['Movimento']['status'] = 'vencido'; 
      } elseif ($dtVc <= $dtVencendo) { 
       $results[$key]['Movimento']['status'] = 'vc15dias'; 
      } else { 
       $results[$key]['Movimento']['status'] = 'aberto'; 
      } 
     } 
     if(isset($val['Movimento']['data_pagamento'])) { 
      $results[$key]['Movimento']['status'] = 'quitado'; 
     } 
    } 
    return $results; 

分頁:

$options = array(
      ... 
      'order' => array('Movimento.status' => 'ASC') 
     ); 
$this->controller->paginate = $options; 
$movimentos = $this->controller->paginate('Movimento'); 

我知道是因爲分頁程序調用之後創建的領域,這並不工作。

我可以使它工作嗎?

回答

0

據我所知,你想data_pagamento和比data_vencimento(有它MySQL的型date?)

,所以你不需要爲你訂購一個afterFind功能,只需使用排序:

'order' => array(
    'Movimento.data_pagamento DESC',//first all rows with not-empty data_pagamento 
    'Movimento.data_vencimento DESC',// first all dates in the furthest future 
) 
+0

因爲「data_pagamento」必須是ASC才能工作,所以我需要95%左右的工作,但是可以。正確的代碼:'order'=> array('Movimento.data_pagamento'=>'ASC','Movimento.data_vencimento'=>'ASC') – Ique 2014-09-04 14:39:28