Cakephp分页自定义顺序

时间:2014-09-01 13:53:36

标签: php cakephp pagination paginator

我需要根据afterFind回调中创建的字段($results[$key]['Movimento']['status'])设置我的寄存器的分页顺序。

一个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');

我知道这不起作用,因为该字段是在分页器调用之后创建的。

我可以让它发挥作用吗?

1 个答案:

答案 0 :(得分:0)

据我了解,您希望按data_pagamento排序,而不是data_vencimento排序(是否为mysql-type date?)

所以你不需要你的afterFind-function进行订购,只需使用:

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