CakePHP:值未保存到Db

时间:2013-01-31 03:52:06

标签: php mysql cakephp

我正在尝试在我自己的应用程序中使用Andras Kende的CakePHP Shopping Cart,但我无法理解为什么以下更改不起作用。

在订单处理结束时,在paypal和所有内容之后,当用户即将完成交易时,我想,如果用户是会员/登录,请使用$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行,我通过执行以下操作将登录的用户ID添加到$order数组:

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

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

 ["user_id"]=> int(18)

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

我的orders表格结构如下所示:

enter image description here

其中一个订单输入如下:

enter image description here

我没有想法,我不知道该怎么办.....

1 个答案:

答案 0 :(得分:0)

经过多次挖掘后,我决定将Db字段更改为user而不是user_id,并且它有效。为什么不采用user_id。我的用户模型$hasMany = array('Order'=>array('foreignKey' => 'user'))。使用user_id将是标准的。我只是想弄清楚它为什么不允许它。到目前为止修复工作!