Arif Sami Arif Sami - 3 months ago 7
PHP Question

cakephp how to save data in different table

my controller: productsController.php

public function buyProduct(){
// some functionaliy and returns
$data = array(
'user_id' => $this->Auth->user('id'),
'product_id' => 11,
'trans_id' => 12,
'auth_code' => 13
);
App::uses('Order', 'Model');
$this->Order->create();
$this->Order->save($data);


Now i want to save this $data to table name order how can i do this

}

Answer

I assumed that you are using CakePHP 2.x. Try something like this:

public function buyProduct(){           
    $data['Order'] = array(
        'user_id' => $this->Auth->user('id'),
        'product_id' => 11,
        'trans_id' => 12,
        'auth_code' => 13
    );
    $this->loadModel('Order');
    $this->Order->create();
    $this->Order->save($data);
}
Comments