2

我正在尝试在我自己的应用程序中使用 Andras Kende 的CakePHP 购物车,但我不明白为什么以下更改不起作用。

在订单流程结束时,在贝宝和一切之后,当用户即将完成交易时,如果用户是会员/已登录,我想使用$this->Auth->user('id')获取他们的身份验证 ID 并将其附加到订单这即将完成。对于我剩下的生命,我无法让它发挥作用。这就是我正在做的事情:

我有以下课程:

    public function review() {
    $shop = $this->Session->read('Shop');       
    if(empty($shop)) {
        $this->redirect('/shop/cart');
    }
    if ($this->request->is('post')) {
        $this->loadModel('Order');
        $this->Order->set($this->request->data);
        if($this->Order->validates()) {
            $order = $shop;
            $order['Order']['user_id'] = (int)$this->Auth->user('id');
            $order['Order']['status'] = 1;

            var_dump($order);

            if($shop['Order']['order_type'] == 'paypal') {
                $paypal = $this->Paypal->ConfirmPayment($order['Order']['total']);
                $ack = strtoupper($paypal['ACK']);
                if($ack == 'SUCCESS' || $ack == 'SUCCESSWITHWARNING') {
                    $order['Order']['status'] = 2;
                }
                $order['Order']['authorization'] = $paypal['ACK'];
            }
            $save = $this->Order->saveAll($order, array('validate' => 'first'));
            if($save) {
                $this->set(compact('shop'));
                App::uses('CakeEmail', 'Network/Email');
                $email = new CakeEmail();
                $email->from($this->requestAction('/settings/getSettingValue/ADMIN_EMAIL'))
                        ->cc($this->requestAction('/settings/getSettingValue/ADMIN_EMAIL'))
                        ->to($shop['Order']['email'])
                        ->subject('Shop Order')
                        ->emailFormat('text')
                        ->viewVars(array('shop' => $shop))
                        ->send();
                $this->redirect(array('action' => 'success'));
            } else {
                $errors = $this->Order->invalidFields();
                $this->set(compact('errors'));
            }
        }           
    }
    if(($shop['Order']['order_type'] == 'paypal') && !empty($shop['Paypal']['Details'])) {
        $shop['Order']['first_name'] = $shop['Paypal']['Details']['FIRSTNAME'];
        $shop['Order']['last_name'] = $shop['Paypal']['Details']['LASTNAME'];
        $shop['Order']['email'] = $shop['Paypal']['Details']['EMAIL'];
        $shop['Order']['phone'] = '888-888-8888';
        $shop['Order']['billing_address'] = $shop['Paypal']['Details']['SHIPTOSTREET'];
        $shop['Order']['billing_address2'] = '';
        $shop['Order']['billing_city'] = $shop['Paypal']['Details']['SHIPTOCITY'];
        $shop['Order']['billing_zip'] = $shop['Paypal']['Details']['SHIPTOZIP'];
        $shop['Order']['billing_state'] = $shop['Paypal']['Details']['SHIPTOSTATE'];
        $shop['Order']['billing_country'] = $shop['Paypal']['Details']['SHIPTOCOUNTRYNAME'];
        $shop['Order']['shipping_address'] = $shop['Paypal']['Details']['SHIPTOSTREET'];
        $shop['Order']['shipping_address2'] = '';
        $shop['Order']['shipping_city'] = $shop['Paypal']['Details']['SHIPTOCITY'];
        $shop['Order']['shipping_zip'] = $shop['Paypal']['Details']['SHIPTOZIP'];
        $shop['Order']['shipping_state'] = $shop['Paypal']['Details']['SHIPTOSTATE'];
        $shop['Order']['shipping_country'] = $shop['Paypal']['Details']['SHIPTOCOUNTRYNAME'];
        $shop['Order']['order_type'] = 'paypal';
        $this->Session->write('Shop.Order', $shop['Order']);
    }
    $this->set(compact('shop'));
}

在第 11 行,我$order通过执行以下操作将登录的用户 ID 添加到数组中:

$order['Order']['user_id'] = (int)$this->Auth->user('id');

此时,如果我执行 a var_dump($order),我将看到当前登录用户的任何用户 ID,在本例中为18。所以我知道我分配正确。

 ["user_id"]=> int(18)

到这里为止一切都很好。但是,当我检查我的数据库时,该user_id字段为 NULL。18没有被保存为添加到数组中。我做了很多事情,我看不出我做错了什么。请帮忙!

我的orders表结构如下所示:

在此处输入图像描述

其中一个订单条目如下所示:

在此处输入图像描述

我的想法用完了,我不知道该怎么办......

4

1 回答 1

0

经过一番挖掘后,我决定将 Db 字段更改为,user而不是user_id它工作。为什么不服用user_id。我的用户模型$hasMany = array('Order'=>array('foreignKey' => 'user'))。使用user_id. 我只是无法弄清楚为什么它不允许它。到目前为止,修复工作!

于 2013-04-26T19:57:29.133 回答