Single Model for Two related tables

can we make A single model for Two tables(related, has-one) and How?

After Save of First table foreign key goes to second table and some detail

also?

OR

any way to insert data in two tables using single form? by using after save of first Model.

can we make A single model for Two tables(related, has-one) and How?

I dont think so, never tried but, why do you want such feature? Selection maybe? IF that is so, you can create a VIEW on your Database and then create a Model to work on AR but… I do not have a clue if, or not, when you save the AR you actually save on the VIEW and the connected tables RESPECTIVELY.

any way to insert data in two tables using single form? by using after save of first Model.

You can do it using event onAfterSave() for the model. In this function you can insert the data to the other table by using DAO or its correspondent Model (AR).

Cheers

i want a single form to insert values into two tables related

one primary goes as foreign in second table

is there any way to do this?

help me i m stuck at this point…

I suggest you to use two models $model1 and $model2. Just pass both to a view and create inputs as usual:




$form->textField($model1, 'attribute1'); // or CHtml::activeTextField($model1, 'attribute1');

$form->textField($model2, 'attribute2'); // or CHtml::activeTextField($model2, 'attribute2');



After a form is submitted, save your models using $model1->save() and $model2->save().

There is a more “advanced” way to do that using form builder & nested forms, maybe you’ll like it more.

You may also try this how to

thanks for the Help

thanks man i’ll take a look

Thanks a lot Both andy_s and fouss

I got it working

Hi, Abrar.

How did you get it working? Can u tell me?

U use two models or one?

I think using view is the best solution … I struggled two days in that problem until start to use views as Antonio Ramirez suggest

Thanks Antonio Ramirez