<?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?> <?php $username = Yii::$app->user->identity->username; $users = User::find()->all(); $scholars = Scholar::find()->all(); $model = new Subject(); $modelCustomer = new Grade(); $modelsAddress = [new Grade()]; foreach ($users as $user) { foreach ($scholars as $scholar) { if ($user->username == $username && $user->id == $scholar->scholar_user_id) { $modelCustomer->subject_scholar_scholar_id = $scholar->scholar_id; if ($modelCustomer->load(Yii::$app->request->post())) { $modelsAddress = GroupGrade::createMultiple(Grade::classname()); GroupGrade::loadMultiple($modelsAddress, Yii::$app->request->post()); // ajax validation if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ArrayHelper::merge(ActiveForm::validateMultiple($modelsAddress), ActiveForm::validate($modelCustomer)); } // validate all models $valid = $modelCustomer->validate(); $valid = GroupGrade::validateMultiple($modelsAddress) && $valid; if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { // if($flag = $modelCustomer->save(false)){ foreach ($modelsAddress as $modelAddress) { $modelAddress->subject_scholar_scholar_id = $modelCustomer->subject_scholar_scholar_id;
public function actionGroupcreate() { $modelCustomer = new Grade(); $modelsAddress = [new Grade()]; if ($modelCustomer->load(Yii::$app->request->post())) { $modelsAddress = GroupGrade::createMultiple(Grade::classname()); GroupGrade::loadMultiple($modelsAddress, Yii::$app->request->post()); // ajax validation if (Yii::$app->request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return ArrayHelper::merge(ActiveForm::validateMultiple($modelsAddress), ActiveForm::validate($modelCustomer)); } // validate all models $valid = $modelCustomer->validate(); $valid = GroupGrade::validateMultiple($modelsAddress) && $valid; if ($valid) { $transaction = \Yii::$app->db->beginTransaction(); try { // if($flag = $modelCustomer->save(false)){ foreach ($modelsAddress as $modelAddress) { $modelAddress->subject_scholar_scholar_id = $modelCustomer->subject_scholar_scholar_id; $modelAddress->grade_school_year_start = $modelCustomer->grade_school_year_start; $modelAddress->grade_school_year_end = $modelCustomer->grade_school_year_end; $selectSchool = ArrayHelper::map(Scholar::find()->where(['scholar_id' => $modelAddress->subject_scholar_scholar_id])->all(), 'school_school_id', 'school_school_id'); $schoolID = array_values($selectSchool)[0]; $modelAddress->subject_scholar_school_school_id = $schoolID; if (!($flag = $modelAddress->save(false))) { $transaction->rollBack(); break; } } // } if ($flag) { if ($modelCustomer->subject_subject_id == null) { $sql = "DELETE FROM grade WHERE subject_subject_id is null;"; Yii::$app->db->createCommand($sql)->execute(); } $transaction->commit(); return $this->redirect(['index']); } } catch (Exception $e) { $transaction->rollBack(); } } } return $this->render('groupcreate', ['modelCustomer' => $modelCustomer, 'modelsAddress' => empty($modelsAddress) ? [new Grade()] : $modelsAddress]); }