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

Cakephp save()在一个字段上失败,php,cakephp,cakephp-2.5,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'])) { $f

我的代码是:

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
        )

)
顺便说一句,当我检查我的表时,所有字段都被正确保存,除了货币字段是空的。此字段为:“货币”字符3不为空


知道为什么保存时我的表中此字段为空吗?

我认为您必须替换:

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


因为如果您的$this->request->数据格式良好,它应该在数组PaypalTransaction中

对不起,我自己得到了解决方案,我将它发布在这里,以防其他人需要帮助:


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

可能不是问题的原因,但我认为Cake不支持数据库类型CHAR。您应该使用VARCHAR3。谢谢,但这不是问题所在,我添加了解决方案作为答案。它说我必须等待2天才能接受它。我会的。
if (!isset($fields['PaypalTransaction']['currency'])) {
    $fields['PaypalTransaction']['currency'] = 'EUR';
}