0
votes

How to post data from one form to two database table in same data base? where the fields have the same name in both tables.

For instance I have the following tables:

a {id, type, name, age}
b{id,a_id,type,name}

So basically I am trying to have the following values to be posted to both tables: type and name.

where the a_id in b is the id of a.

I am using cakephph. Can anyone please help.

Thanks.

2
You may just need to manipulate your data pre-save. Just copy the values to the separate data array to save. - Barry Chapman

2 Answers

0
votes

if you want to get the values from table a then use it like..

a.type,a.name,a.age

if you want to get the values from table b then use it like..

b.a_id,b.type,b.name,b.age

0
votes

Let's say you have two models called Store and Product, and it doesn't matter if they're related or not.

On your View you could do this:

echo $this->Form->create('Store');    
echo $this->Form->input('Store.name');    
echo $this->Form->input('Store.location');    
echo $this->Form->input('Product.name');    
echo $this->Form->input('Product.type');    
echo $this->Form->end('Save');

And on your StoresController you should do this, to save both models data:

// CakePHP 1.3    
$this->Store->save($this->data['Store']);
$this->Product->save($this->data['Product']);

// CakePHP 2.0  
$this->Store->save($this->request->data['Store']);
$this->Product->save($this->request->data['Product']);

If they're related models, you could save both with one simple call:

$this->Product->saveAll($this->data); // CakePHP 1.3

$this->Product->saveAssociated($this->request->data); // CakePHP 2.0

Do not forget to check the documentation:

CakePHP 1.3 - http://book.cakephp.org/1.3/en/view/1031/Saving-Your-Data

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