选择并包含在cakephp查询中不起作用

时间:2017-01-11 09:54:42

标签: php cakephp cakephp-3.0

当我在没有select()的情况下进行查询时,我得到contain() [所有者]数组,但是使用带有参数的select(),它不包含[owner]数组。使用select和contains同时存在任何问题吗?

$payments = $this->Payments->find()
                        ->select($this->Payments)
                        ->select(['tot_amount'=>'sum(Payments.amount)'])
                        ->select(['tot_owner_amt'=>'sum(Payments.owner_amt)'])
                        ->select(['tot_admin_amt'=>'sum(Payments.admin_amt)'])
                        ->contain(['Owners'])
                        ->group(['Payments.owner_id'])
                        ->all();
        debug($payments); die();

调试数组

 \src\Controller\PaymentsController.php (line 72)

object(Cake\ORM\ResultSet) {

    'items' => [
        (int) 0 => object(App\Model\Entity\Payment) {

            'id' => (int) 4,
            'user_id' => (int) 4,
            'owner_id' => (int) 5,
            'wifi_id' => (int) 2,
            'payment_unique_key' => 'pay-9999',
            'payment_type' => 'paypal',
            'amount' => (float) 10,
            'owner_amt' => (float) 1,
            'admin_amt' => (float) 9,
            'ispaid' => false,
            'paid_at' => object(Cake\I18n\FrozenTime) {

                'time' => '2017-01-10T11:21:00+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            'is_refund' => false,
            'isactive' => true,
            'isdel' => null,
            'created' => object(Cake\I18n\FrozenTime) {

                'time' => '2017-01-10T11:22:24+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            'modified' => object(Cake\I18n\FrozenTime) {

                'time' => '2017-01-10T11:30:26+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            'tot_amount' => '10.00',
            'tot_owner_amt' => '1.00',
            'tot_admin_amt' => '9.00',
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'Payments'

        },
        (int) 1 => object(App\Model\Entity\Payment) {

            'id' => (int) 1,
            'user_id' => (int) 1,
            'owner_id' => (int) 6,
            'wifi_id' => (int) 1,
            'payment_unique_key' => 'adfasdf',
            'payment_type' => 'paypal',
            'amount' => (float) 500,
            'owner_amt' => (float) 50,
            'admin_amt' => (float) 449,
            'ispaid' => false,
            'paid_at' => object(Cake\I18n\FrozenTime) {

                'time' => '2017-01-10T11:21:00+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            'is_refund' => false,
            'isactive' => true,
            'isdel' => null,
            'created' => object(Cake\I18n\FrozenTime) {

                'time' => '2017-01-10T11:22:24+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            'modified' => object(Cake\I18n\FrozenTime) {

                'time' => '2017-01-10T11:30:26+00:00',
                'timezone' => 'UTC',
                'fixedNowTime' => false

            },
            'tot_amount' => '2000.00',
            'tot_owner_amt' => '200.00',
            'tot_admin_amt' => '1798.00',
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'Payments'

        }
    ]

}

我在上面的代码中遗漏了什么吗?提前致谢

2 个答案:

答案 0 :(得分:1)

如果您没有选择所有者数据,那么它不会获取所有者?很简单。您只选择付款,就像使用付款一样添加所有者。

答案 1 :(得分:0)

需要选择select中的所有者数组,例如select($this->Payments->Owners),谢谢@burzum

解决方案

$payments = $this->Payments->find()
                        ->select($this->Payments)
                        ->select(['tot_amount'=>'sum(Payments.amount)'])
                        ->select(['tot_owner_amt'=>'sum(Payments.owner_amt)'])
                        ->select(['tot_admin_amt'=>'sum(Payments.admin_amt)'])
                        ->select($this->Payments->Owners)
                        ->contain(['Owners'])
                        ->group(['Payments.owner_id'])
                        ->all();