0

我需要根据$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');

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

我可以让它工作吗?

4

1 回答 1

0

据我了解,您想按排序data_pagamento而不是排序data_vencimento(有 mysql-typedate吗?)

所以你不需要你的 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
)
于 2014-09-02T23:36:23.927 回答