user160820
user160820

Reputation: 15200

CakePHP saving data in multiple tables

I have shop struct like

1: Shop has many Categories

2: Categories have 1 to many Sizes and Products

3: Each product belongs to exactly one Category

4: As Sizes belongs to Categories so each product belonging to a category will have same sizes.

Now my problem is in Product add form.

I have form the contains product's input fields and a selectbox to select the Category.

As soon as I select a category, an Ajax request will be created, which return sizes as inputbox assigned to the selected category. The input boxes will be used to enter the price for each size of the product. (As the input fields will be generated in Ajax, how may i use the form helper?)

Now I want that on clicking the save button, the product information should be stored in product table, and the prices should be saved in

Product_Prices table which contains product_id, size_id, price columns.

How should i name my inputboxs? and how should i call the save method so that the informations are properly saved?

Upvotes: 1

Views: 3083

Answers (1)

Pitsanu Swangpheaw
Pitsanu Swangpheaw

Reputation: 672

To save one-size

//$this->Form->create('Product');
$this->Form->input('category_id');

To save many-size

//$this->Form->create('Shop');

$this->Form->input('category_id');

or use checkbox instead list

$this->Form->input('category_id', array('multiple' => 'checkbox')); 

If you want to save property of another model together with you main model you add model name (alias) as prefix and use saveAssociated() or saveAll().

echo $this->Form->input('ProductPrice.price');

see: http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null-array-options-array

Upvotes: 2

Related Questions