Cakephp save()在一个字段上失败

时间:2015-05-05 14:28:00

标签: php cakephp cakephp-2.5

我的代码是:

public function save_paypal_transaction() {
    if ($this->request->is('post')) {

        $this->loadModel('PaypalTransaction');

        $fields = $this->request->data;
        if (!isset($fields['currency'])) {
            $fields['currency'] = 'EUR';
        }

        $this->log($fields);

        $res = $this->PaypalTransaction->save($fields);

        $this->log(print_r($res, 1));

        if ($res) {
            echo json_encode(array('status' => 'success'));
        } else {
            echo json_encode(array('status' => 'error', 'data' => 'Error while saving into db'));
        }
    } else {
        echo json_encode(array('status' => 'error'));
    }
}

然后,当我检查我的日志时,我有:

2015-05-05 13:59:29 Error: Array
(
    [transaction_id] => xxxxxxx
    [profile_id] => xxxxxx
    [item_name] => xxxxxxx
    [total_price] => 30.00
    [buyer_f_name] => xxxxxxx
    [buyer_l_name] => xxxxxxx
    [buyer_email] => xxxxxxx@hotmail.com
    [date_dt] => 2015-05-05 13:59:29
    [user_id] => 0
    [currency] => EUR
)

2015-05-05 13:59:29 Error: Array
(
    [PaypalTransaction] => Array
        (
            [transaction_id] => xxxxxxx
            [profile_id] => xxxxxxx
            [item_name] => xxxxxxx
            [total_price] => 30.00
            [buyer_f_name] => xxxxxxx
            [buyer_l_name] => xxxxxxx
            [buyer_email] => xxxxxxx@hotmail.com
            [date_dt] => 2015-05-05 13:59:29
            [user_id] => 0
            [currency] => EUR
            [id] => 7807
        )

)

顺便说一句,当我检查我的表时,除了空currency字段外,所有字段都正确保存。此字段为:'currency' CHAR(3) NOT NULL

知道为什么这个字段在我保存的表格中是空的?

2 个答案:

答案 0 :(得分:0)

我认为你必须更换:

if (!isset($fields['currency'])) {
    $fields['currency'] = 'EUR';
}

by:

if (!isset($fields['PaypalTransaction']['currency'])) {
    $fields['PaypalTransaction']['currency'] = 'EUR';
}

如果您的$ this-> request->数据格式正确,则应该在数组中PaypalTransaction

答案 1 :(得分:0)

抱歉,我自己得到了解决方案,如果其他人需要帮助,我会在此处发布:

代码本身没问题,但我只是删除了/ tmp / cache / models中的缓存文件,它解决了这个问题。我想因为货币字段的添加时间晚于表格缓存的生成,所以cakephp缓存在“内存”中没有这个字段。