hi all,
i have a problem in understanding how to INSERT data into multiple tables at once?
i have 2 tables.
table A(id,a1)
table B(id,b1)
i tried the code in controller for create()
public function actionCreate()
{
$model=new A;
$b=new B;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['A'], $_POST['B']))
{
$model->attributes=$_POST['A'];
$b->attributes=$_POST['B'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
if($b->save())
$this->redirect(array('view','id'=>$b->id));
}
// validate BOTH $a and $b
$valid=$model->validate();
$valid=$b->validate() && $valid;
if($valid)
{
// use false parameter to disable validation
$model->save(false);
$b->save(false);
// ...redirect to another page
}
$this->render('create',array(
'b'=>$b,
'model'=>$model,
));
}
i created a from containing both a1 and b1 in it.
the problem is , the data is getting inserted indivdually for both the table.
for example if i insert data for either a1 or b1 , any one is getting inserted.
But if i insert both a1 and b1 then only a1 is getting inserted.
help me please…