Arif Sami
Arif Sami

Reputation: 317

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

}

Upvotes: 0

Views: 288

Answers (2)

Jacek B Budzynski
Jacek B Budzynski

Reputation: 1413

In CakePhp 2.6.11 I use this:

class productsController extends AppController {

    public $uses = array('Order');

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

Upvotes: 1

Bart
Bart

Reputation: 1268

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

Upvotes: 0

Related Questions