public function actionCreate() { $model=new ItemHeader; $suppliers[] = new Supplier; $principal = new ItemPrincipal; // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if(isset($_POST['ItemHeader'])) { $model->attributes=$_POST['ItemHeader']; $selected_branch = $_POST['ItemHeader']['branch_id']; if(isset($_POST['ItemPrincipal'])){ $principal->attributes = $_POST['ItemPrincipal']; } if(isset($_POST['Supplier'])) $suppliers = $_POST['Supplier']; if($model->validate() && $principal->validate()){ $model->itemPrincipal = $principal; $model->suppliers = array_unique($suppliers); if($selected_branch == Branch::ROOT_BRANCH){ //Yii::app()->end(); //create item header for all branches $branches = Branch::model()->findAll(); foreach($branches as $branch){ $bmodel = new ItemHeader; $bmodel->attributes = $model->attributes; $bmodel->suppliers = $model->suppliers; $bmodel->itemPrincipal = $model->itemPrincipal; $bmodel->branch_id = $branch->id; $bmodel->itemPrincipal->save(); $bmodel->save(); } $this->redirect(array('admin','ItemHeader[item_code]'=>$model->item_code)); } else{ $model->itemPrincipal->save(); $model->save(); } $this->redirect(array('view','id'=>$model->id)); } } $this->render('create',array( 'model'=>$model, 'suppliers'=>$suppliers, 'principal'=>$principal, )); }