/** * Creates a new Monitoring1 model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Monitoring1(); $months = Month::find()->all(); $years = Year::find()->all(); $model->user_id = Yii::$app->user->id; if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'months' => $months, 'years' => $years]); } }
/** * Creates a new Monitoring1 model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $modelsStr = [new Mon1str()]; $model = new Monitoring1(); $months = Month::find()->all(); $years = Year::find()->all(); $model->user_id = Yii::$app->user->id; if ($model->load(Yii::$app->request->post())) { $model->date = date('Y-m-d', strtotime($model->date)); if (!Monitoring1::find()->where(['date' => $model->date, 'user_id' => Yii::$app->user->id])->one()) { if ($model->save()) { $modelsStr = Model::createMultiple(Mon1str::classname()); Model::loadMultiple($modelsStr, Yii::$app->request->post()); // ajax validation if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ArrayHelper::merge(ActiveForm::validateMultiple($modelsStr), ActiveForm::validate($model)); } // validate all models $valid = $model->validate(); $valid = Model::validateMultiple($modelsStr) && $valid; if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { if ($flag = $model->save(false)) { foreach ($modelsStr as $modelStr) { $modelStr->monitoring1_id = $model->id; if (!($flag = $modelStr->save(false))) { $transaction->rollBack(); break; } } } if ($flag) { $transaction->commit(); return $this->redirect(['view', 'id' => $model->id]); } } catch (Exception $e) { $transaction->rollBack(); } } } } } return $this->render('create', ['model' => $model, 'months' => $months, 'years' => $years, 'modelsStr' => empty($modelsStr) ? [new Mon1str()] : $modelsStr]); }