コード例 #1
0
ファイル: InsumoController.php プロジェクト: rvences/mopoua
 /**
  * Creates a new Insumo model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Insumo();
     $modelsPresentacion = [new Presentacion()];
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         $modelsPresentacion = Model::createMultiple(Presentacion::className());
         Model::loadMultiple($modelsPresentacion, Yii::$app->request->post());
         // validate all models
         $valid = $model->validate();
         $valid = Model::validateMultiple($modelsPresentacion) && $valid;
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $model->save(false)) {
                     foreach ($modelsPresentacion as $modelPresentacion) {
                         $modelPresentacion->insumo_id = $model->id;
                         if (!($flag = $modelPresentacion->save(false))) {
                             $transaction->rollBack();
                             break;
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id]);
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
         //return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'modelsPresentacion' => empty($modelsPresentacion) ? [new Presentacion()] : $modelsPresentacion]);
     }
     return $this->redirect(['index']);
 }