doushi9444 2014-09-01 13:53
浏览 83
已采纳

Cakephp分页自定义顺序

I need to set the pagination order of my register based on field ($results[$key]['Movimento']['status']) created in afterFind callback.

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;

Pagination:

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

I know this does not work because the field is created after the paginator call.

Can I make it work?

  • 写回答

1条回答 默认 最新

  • douren7921 2014-09-02 23:36
    关注

    as I understand, you want to sort by data_pagamento and than by data_vencimento (has it the mysql-type date?)

    so you don't need your afterFind-function for ordering, simply use:

    '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
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭