user160820
user160820

Reputation: 15200

Saving data in multiple tables

I have a Products table and a Products_Prices tables. The Products_Prices table has these fields product_id, size_id, price

in ProductsController's add action I am getting the data like. The Product save is working

but i don't know that after saving the product how should I get the ID of newly created product and how to save the price info.

array(
    'id' => '',
    'name' => 'Pizza Salami',
    'description' => 'some description here',
    'active' => '1',
    'offered' => '1',
    'category_id' => '15',
    'Price' => array(
        (int) 0 => array(
            'size_id' => '2',
            'price' => '5'
        ),
        (int) 1 => array(
            'size_id' => '3',
            'price' => '6'
        ),
        (int) 2 => array(
            'size_id' => '4',
            'price' => '7'
        ),
        (int) 3 => array(
            'size_id' => '5',
            'price' => '9'
        )
    )
)

Upvotes: 0

Views: 1081

Answers (2)

ammu
ammu

Reputation: 386

If saveAll and saveAssociated doesnt work for inserting rows into table, you can use $this->ModelName->getLastInsertID(); to get last inserted id in your table Products.

Upvotes: 1

noslone
noslone

Reputation: 1289

Have you checked the documentation?

You can get the id with $id = $this->Model->id;

You can also use $this->Model->saveAssociated($this->request->data); to save multiple model data at once.

Upvotes: 1

Related Questions