Cakephp save() 在一个字段上失败
Cakephp save() fails on one field
我的代码是:
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
)
)
顺便说一句,当我检查我的 table 时,除了 currency
字段为空之外,所有字段都已正确保存。该字段是:'currency' CHAR(3) NOT NULL
知道为什么我的 table 保存时此字段为空吗?
我认为你必须更换:
if (!isset($fields['currency'])) {
$fields['currency'] = 'EUR';
}
作者:
if (!isset($fields['PaypalTransaction']['currency'])) {
$fields['PaypalTransaction']['currency'] = 'EUR';
}
因为如果您的 $this->request->data 格式正确,它应该在数组 PaypalTransaction
中
抱歉,我自己找到了解决方案,我post在这里以防其他人需要帮助:
代码本身没问题,但我只是删除了 /tmp/cache/models 中的缓存文件并解决了问题。我猜因为货币字段是在生成表缓存之后添加的,所以 cakephp 缓存在 'memory'.
中没有这个字段
我的代码是:
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
)
)
顺便说一句,当我检查我的 table 时,除了 currency
字段为空之外,所有字段都已正确保存。该字段是:'currency' CHAR(3) NOT NULL
知道为什么我的 table 保存时此字段为空吗?
我认为你必须更换:
if (!isset($fields['currency'])) {
$fields['currency'] = 'EUR';
}
作者:
if (!isset($fields['PaypalTransaction']['currency'])) {
$fields['PaypalTransaction']['currency'] = 'EUR';
}
因为如果您的 $this->request->data 格式正确,它应该在数组 PaypalTransaction
中抱歉,我自己找到了解决方案,我post在这里以防其他人需要帮助:
代码本身没问题,但我只是删除了 /tmp/cache/models 中的缓存文件并解决了问题。我猜因为货币字段是在生成表缓存之后添加的,所以 cakephp 缓存在 'memory'.
中没有这个字段