1

My code is:

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'));
    }
}

Then when I check my logs I have:

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
        )

)

By the way, when I check my table, all fields are saved correctly excepts the currency fields who is empty. This field is : 'currency' CHAR(3) NOT NULL

Any idea why this field is empty in my table on save ?

zeflex
  • 1,487
  • 1
  • 14
  • 29
  • 1
    Probably not the cause of your problem, but I don't believe Cake supports the database type CHAR. You should use VARCHAR(3) instead. – drmonkeyninja May 05 '15 at 14:40

2 Answers2

0

I think you must replace :

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

by :

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

cause if your $this->request->data is in a good format, it should be in a array PaypalTransaction

Mato
  • 39
  • 4
0

Sorry I got the solution by myself, I post it here in case of somebody else need help:

The code itself is ok , but I simply deleted cache files in /tmp/cache/models and it fixed the issue. I guess as the currency field was added later than the tables cache was generated, the cakephp cache didn't have this field in 'memory'.

zeflex
  • 1,487
  • 1
  • 14
  • 29