i want to have a single form to collect data for two models. I already viewed the documentation ‘How to use a single form to collect data for two or more models?’. I have incoperated it in my code but its not working.
The following is my controller code
public function actionCreate()
{
$model=new BusType;
$model2=new BusProducts;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['BusType'],$_POST['BusProducts']))
{
$model->attributes=$_POST['BusType'];
$model2->attributes=$_POST['BusProducts'];
if($model->save())
{
$model2->bus_product_id = $model->bus_type_id;
$this->redirect(array('view','id'=>$model->bus_type_id));
}
}
$this->render('create',array(
'model'=>$model,
'model2'=>$model2,
));
}
I also made the changes in _form i.e echo $form->errorSummary(array($model,$model2)); and the changes in create i.e $this->renderPartial(’_form’, array(‘model’=>$model,‘model2’=>$model2));.
But the code is still not working. Do i need to make changes anywhere else?
I have another issue related to the same. I want to make some inserts in model2. Currently i am able to isert the FK. How do i insert other data? Am i supposedd to use aftersave() for the same? If so can u please tell me how to go about that? Thanks
what i meant was a model attribute with several data.
The thing is basically when i submit a form i want the inserts to go in 2 different tables.
If u have read the description of my models, what i want is in the 2nd table ‘multiple’ entries of subcategories for every category. How do i go about this?