如何在 cakephp 中使用自定义查询分页

How to use pagination with custom query in cakephp

我正在从事邮政服务项目的报告工作,我想在分页的帮助下显示数据。我知道我们可以覆盖模型中的分页功能。我已经尝试过了,但对我没有用。 我有 BillReport 模型,我在其中创建了函数。下面是我的自定义查询,它显示针对一次付款的多个账单意味着付款有很多账单。我可以对 Payment 进行分页,但我想对 Bill wise 进行分页。 我已经在 Billpaymentmap table 中映射了账单和付款,并且从那个 table 我得到了以下查询的数组。

我想对与 Payment 关联的相关模型进行分页。付款 Hasmany 法案。

$reversal = $this->query("SELECT Pay.*,C.id,C.name,U.id,U.username,Bill.account_no,Bill.id, Pay.*,PMap.data,PMap.paid_amount FROM dbo_payments As Pay INNER JOIN dbo_reverse_payments As RPay ON RPay.payment_id = Pay.id INNER JOIN dbo_payment_bill_maps As PMap ON PMap.payment_id = RPay.payment_id INNER JOIN dbo_users U ON U.id = Pay.cashier_id INNER JOIN dbo_clients As C ON C.id = U.client_id INNER JOIN dbo_bills As Bill ON Bill.id = PMap.bill_id WHERE Pay.reversal = 1 and $conditions ");


Array
(
[0] => Array
    (
        [Pay] => Array
            (
                [id] => 10
                [location_id] => 133
                [cashier_id] => 5
                [client_id] => 2
                [dealer_id] => 31
                [receipt_no] => 58130
                [pay_date] => 2015-02-18 12:42:28
                [amount_paid] => 3715
                [total_paid] => 3715
                [change_due] => 0
                [pay_method] => CA
                [bank] => 
                [ref_no] => 
                [payment_type] => Bill
                [reversal_fee] => 
                [reversal_code] => 
                [reversal] => 1
                [reversal_date] => 
                [reversed_by_id] => 
            )

        [C] => Array
            (
                [id] => 31
                [name] => GAMPOST
            )

        [U] => Array
            (
                [id] => 5
                [username] => admin
            )

        [Bill] => Array
            (
                [account_no] => 1910009
                [id] => 1037079
            )

        [RPay] => Array
            (
                [id] => 1
                [payment_id] => 10
                [user_id] => 5
                [reason] => LIC
                [reversal_fee] => 
                [created] => 2015-02-18 13:06:28
            )

        [PMap] => Array
            (
                [data] => a:3:{s:4:"name";s:6:"Ganesh";s:7:"address";s:17:"Test address 1sdk";s:11:"paymentType";s:4:"BILL";}
                [paid_amount] => 3215.00
            )

    )

[1] => Array
    (
        [Pay] => Array
            (
                [id] => 10
                [location_id] => 133
                [cashier_id] => 5
                [client_id] => 2
                [dealer_id] => 31
                [receipt_no] => 58130
                [pay_date] => 2015-02-18 12:42:28
                [amount_paid] => 3715
                [total_paid] => 3715
                [change_due] => 0
                [pay_method] => CA
                [bank] => 
                [ref_no] => 
                [payment_type] => Bill
                [reversal_fee] => 
                [reversal_code] => 
                [reversal] => 1
                [reversal_date] => 
                [reversed_by_id] => 
            )

        [C] => Array
            (
                [id] => 31
                [name] => GAMPOST
            )

        [U] => Array
            (
                [id] => 5
                [username] => admin
            )

        [Bill] => Array
            (
                [account_no] => 546865468
                [id] => 1037162
            )

        [RPay] => Array
            (
                [id] => 1
                [payment_id] => 10
                [user_id] => 5
                [reason] => LIC
                [reversal_fee] => 
                [created] => 2015-02-18 13:06:28
            )

        [PMap] => Array
            (
                [data] => a:3:{s:4:"name";s:38:"Amit  Trivedi Trivedi Trivedi  Trivedi";s:7:"address";s:6:"Mamura";s:11:"paymentType";s:4:"BILL";}
                [paid_amount] => 500.00
            )

    )

[2] => Array
    (
        [Pay] => Array
            (
                [id] => 7
                [location_id] => 133
                [cashier_id] => 5
                [client_id] => 2
                [dealer_id] => 31
                [receipt_no] => 82053
                [pay_date] => 2015-02-18 12:29:21
                [amount_paid] => 8380
                [total_paid] => 8380
                [change_due] => 0
                [pay_method] => CA
                [bank] => 
                [ref_no] => 
                [payment_type] => Bill
                [reversal_fee] => 
                [reversal_code] => 
                [reversal] => 1
                [reversal_date] => 
                [reversed_by_id] => 
            )

        [C] => Array
            (
                [id] => 31
                [name] => GAMPOST
            )

        [U] => Array
            (
                [id] => 5
                [username] => admin
            )

        [Bill] => Array
            (
                [account_no] => 1910001
                [id] => 1037071
            )

        [RPay] => Array
            (
                [id] => 2
                [payment_id] => 7
                [user_id] => 5
                [reason] => LF
                [reversal_fee] => 
                [created] => 2015-02-18 13:06:43
            )

        [PMap] => Array
            (
                [data] => a:3:{s:4:"name";s:6:"Tushar";s:7:"address";s:17:"Test address 1sdk";s:11:"paymentType";s:4:"BILL";}
                [paid_amount] => 4665.00
            )

    )

[3] => Array
    (
        [Pay] => Array
            (
                [id] => 7
                [location_id] => 133
                [cashier_id] => 5
                [client_id] => 2
                [dealer_id] => 31
                [receipt_no] => 82053
                [pay_date] => 2015-02-18 12:29:21
                [amount_paid] => 8380
                [total_paid] => 8380
                [change_due] => 0
                [pay_method] => CA
                [bank] => 
                [ref_no] => 
                [payment_type] => Bill
                [reversal_fee] => 
                [reversal_code] => 
                [reversal] => 1
                [reversal_date] => 
                [reversed_by_id] => 
            )

        [C] => Array
            (
                [id] => 31
                [name] => GAMPOST
            )

        [U] => Array
            (
                [id] => 5
                [username] => admin
            )

        [Bill] => Array
            (
                [account_no] => 1910009
                [id] => 1037079
            )

        [RPay] => Array
            (
                [id] => 2
                [payment_id] => 7
                [user_id] => 5
                [reason] => LF
                [reversal_fee] => 
                [created] => 2015-02-18 13:06:43
            )

        [PMap] => Array
            (
                [data] => a:3:{s:4:"name";s:6:"Ganesh";s:7:"address";s:17:"Test address 1sdk";s:11:"paymentType";s:4:"BILL";}
                [paid_amount] => 3215.00
            )

    )

[4] => Array
    (
        [Pay] => Array
            (
                [id] => 7
                [location_id] => 133
                [cashier_id] => 5
                [client_id] => 2
                [dealer_id] => 31
                [receipt_no] => 82053
                [pay_date] => 2015-02-18 12:29:21
                [amount_paid] => 8380
                [total_paid] => 8380
                [change_due] => 0
                [pay_method] => CA
                [bank] => 
                [ref_no] => 
                [payment_type] => Bill
                [reversal_fee] => 
                [reversal_code] => 
                [reversal] => 1
                [reversal_date] => 
                [reversed_by_id] => 
            )

        [C] => Array
            (
                [id] => 31
                [name] => GAMPOST
            )

        [U] => Array
            (
                [id] => 5
                [username] => admin
            )

        [Bill] => Array
            (
                [account_no] => 546865468
                [id] => 1037162
            )

        [RPay] => Array
            (
                [id] => 2
                [payment_id] => 7
                [user_id] => 5
                [reason] => LF
                [reversal_fee] => 
                [created] => 2015-02-18 13:06:43
            )

        [PMap] => Array
            (
                [data] => a:3:{s:4:"name";s:38:"Amit  Trivedi Trivedi Trivedi  Trivedi";s:7:"address";s:6:"Mamura";s:11:"paymentType";s:4:"BILL";}
                [paid_amount] => 500.00
            )

    )

)

我用谷歌搜索但没有得到答案。谁能帮我解决这个问题。

感谢回复。 我经历了下面 link 。我给了 limit => 3 。它为我提供了最多 3 笔付款数据,而不是针对一笔付款的多次付款的账单。 我需要对 'PaymentBillMap' 数据进行分页。

public $paginate = array('limit'=>'3',
                    'order'=>'Payment.pay_date',
                    'fields'=>array('Payment.id','Payment.location_id','Payment.dealer_id'));

public function trans_list_service(){
    $params = $this->__commonopr();
    $this->loadModel('Payment');
    $this->loadModel('PaymentBillMap');


    $conditions = array('Payment.location_id ' => 133);
    // apply the custom conditions to the pagination
    $payments = $this->paginate('Payment',$conditions);
    $this->set('payments', $payments);
pr($payments);

} Custom Query Pagination Cakephp

Array
(
[0] => Array
    (
        [Payment] => Array
            (
                [id] => 1
                [location_id] => 133
                [dealer_id] => 31
            )

        [PaymentBillMap] => Array
            (
                [0] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037153
                        [paid_amount] => 4656.00
                        [payment_type] => BILL
                        [trans_no] => 24862
                        [data] => a:3:{s:4:"name";s:6:"Ganesh";s:7:"address";s:7:"Dholpur";s:11:"paymentType";s:4:"BILL";}
                    )

                [1] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037154
                        [paid_amount] => 4665.00
                        [payment_type] => BILL
                        [trans_no] => 48280
                        [data] => a:3:{s:4:"name";s:5:"AMITA";s:7:"address";s:10:"Ganganagar";s:11:"paymentType";s:4:"BILL";}
                    )

                [2] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037155
                        [paid_amount] => 5787.00
                        [payment_type] => BILL
                        [trans_no] => 45996
                        [data] => a:3:{s:4:"name";s:9:"duryodhan";s:7:"address";s:5:"Jalor";s:11:"paymentType";s:4:"BILL";}
                    )

                [3] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037156
                        [paid_amount] => 6688.00
                        [payment_type] => BILL
                        [trans_no] => 64019
                        [data] => a:3:{s:4:"name";s:6:"Dharaj";s:7:"address";s:8:"Jhalawar";s:11:"paymentType";s:4:"BILL";}
                    )

                [4] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037157
                        [paid_amount] => 8795.00
                        [payment_type] => BILL
                        [trans_no] => 21308
                        [data] => a:3:{s:4:"name";s:4:"Amit";s:7:"address";s:9:"Jhunjhunu";s:11:"paymentType";s:4:"BILL";}
                    )

                [5] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037158
                        [paid_amount] => 5654.00
                        [payment_type] => BILL
                        [trans_no] => 10980
                        [data] => a:3:{s:4:"name";s:5:"Gaesh";s:7:"address";s:4:"Pali";s:11:"paymentType";s:4:"BILL";}
                    )

                [6] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037159
                        [paid_amount] => 3456.00
                        [payment_type] => BILL
                        [trans_no] => 79988
                        [data] => a:3:{s:4:"name";s:4:"Jitu";s:7:"address";s:14:"Sawai Madhopur";s:11:"paymentType";s:4:"BILL";}
                    )

                [7] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037160
                        [paid_amount] => 4664.00
                        [payment_type] => BILL
                        [trans_no] => 26314
                        [data] => a:3:{s:4:"name";s:6:"Rajesh";s:7:"address";s:6:"Sirohi";s:11:"paymentType";s:4:"BILL";}
                    )

                [8] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037161
                        [paid_amount] => 3565.00
                        [payment_type] => BILL
                        [trans_no] => 40148
                        [data] => a:3:{s:4:"name";s:4:"Gani";s:7:"address";s:5:"Baran";s:11:"paymentType";s:4:"BILL";}
                    )

                [9] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037148
                        [paid_amount] => 3442.00
                        [payment_type] => BILL
                        [trans_no] => 48235
                        [data] => a:3:{s:4:"name";s:4:"Jitu";s:7:"address";s:5:"Ajmer";s:11:"paymentType";s:4:"BILL";}
                    )

                [10] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037149
                        [paid_amount] => 4335.00
                        [payment_type] => BILL
                        [trans_no] => 71496
                        [data] => a:3:{s:4:"name";s:4:"amer";s:7:"address";s:5:"Alwar";s:11:"paymentType";s:4:"BILL";}
                    )

                [11] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037150
                        [paid_amount] => 3535.00
                        [payment_type] => BILL
                        [trans_no] => 93135
                        [data] => a:3:{s:4:"name";s:6:"Dilip[";s:7:"address";s:9:"Bharatpur";s:11:"paymentType";s:4:"BILL";}
                    )

                [12] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037151
                        [paid_amount] => 6453.00
                        [payment_type] => BILL
                        [trans_no] => 46714
                        [data] => a:3:{s:4:"name";s:7:"Suvarna";s:7:"address";s:11:"Chittorgarh";s:11:"paymentType";s:4:"BILL";}
                    )

                [13] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037152
                        [paid_amount] => 5323.00
                        [payment_type] => BILL
                        [trans_no] => 43007
                        [data] => a:3:{s:4:"name";s:5:"Datta";s:7:"address";s:5:"Churu";s:11:"paymentType";s:4:"BILL";}
                    )

                [14] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037136
                        [paid_amount] => 6456.00
                        [payment_type] => BILL
                        [trans_no] => 27701
                        [data] => a:3:{s:4:"name";s:6:"Faizan";s:7:"address";s:9:"Dungarpur";s:11:"paymentType";s:4:"BILL";}
                    )

                [15] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037137
                        [paid_amount] => 7657.00
                        [payment_type] => BILL
                        [trans_no] => 29162
                        [data] => a:3:{s:4:"name";s:6:"Shamli";s:7:"address";s:7:"Jodhpur";s:11:"paymentType";s:4:"BILL";}
                    )

                [16] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037138
                        [paid_amount] => 8688.00
                        [payment_type] => BILL
                        [trans_no] => 77181
                        [data] => a:3:{s:4:"name";s:6:"Tushat";s:7:"address";s:4:"Kota";s:11:"paymentType";s:4:"BILL";}
                    )

                [17] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037139
                        [paid_amount] => 4657.00
                        [payment_type] => BILL
                        [trans_no] => 75049
                        [data] => a:3:{s:4:"name";s:5:"Danav";s:7:"address";s:7:"Udaipur";s:11:"paymentType";s:4:"BILL";}
                    )

                [18] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037140
                        [paid_amount] => 6886.00
                        [payment_type] => BILL
                        [trans_no] => 48850
                        [data] => a:3:{s:4:"name";s:6:"Kailas";s:7:"address";s:7:"Bikaner";s:11:"paymentType";s:4:"BILL";}
                    )

                [19] => Array
                    (
                        [payment_id] => 1
                        [bill_id] => 1037141
                        [paid_amount] => 7965.00
                        [payment_type] => BILL
                        [trans_no] => 11684
                        [data] => a:3:{s:4:"name";s:7:"Paravat";s:7:"address";s:5:"Dausa";s:11:"paymentType";s:4:"BILL";}
                    )

            )

    )

[1] => Array
    (
        [Payment] => Array
            (
                [id] => 2
                [location_id] => 133
                [dealer_id] => 31
            )

        [PaymentBillMap] => Array
            (
                [0] => Array
                    (
                        [payment_id] => 2
                        [bill_id] => 1037162
                        [paid_amount] => 500.00
                        [payment_type] => BILL
                        [trans_no] => 62066
                        [data] => a:3:{s:4:"name";s:37:"Amit  Trivedi Trivedi Trivedi Trivedi";s:7:"address";s:6:"Mamura";s:11:"paymentType";s:4:"BILL";}
                    )

                [1] => Array
                    (
                        [payment_id] => 2
                        [bill_id] => 1037079
                        [paid_amount] => 3215.00
                        [payment_type] => BILL
                        [trans_no] => 41810
                        [data] => a:3:{s:4:"name";s:6:"Ganesh";s:7:"address";s:17:"Test address 1sdk";s:11:"paymentType";s:4:"BILL";}
                    )

            )

    )

[2] => Array
    (
        [Payment] => Array
            (
                [id] => 3
                [location_id] => 133
                [dealer_id] => 31
            )

        [PaymentBillMap] => Array
            (
                [0] => Array
                    (
                        [payment_id] => 3
                        [bill_id] => 1037162
                        [paid_amount] => 500.00
                        [payment_type] => BILL
                        [trans_no] => 74439
                        [data] => a:3:{s:4:"name";s:38:"Amit  Trivedi Trivedi Trivedi  Trivedi";s:7:"address";s:6:"Mamura";s:11:"paymentType";s:4:"BILL";}
                    )

            )

    )

)