/**
  * Creates a new TeacherHasDiscipline model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new TeacherHasDiscipline();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 /**
  * Updates an existing GroupHasDiscipline model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         foreach (Yii::$app->request->post()['GroupHasDiscipline']['teacherHasDiscipline']['teacher_id'] as $teacher) {
             if (!TeacherHasDiscipline::find()->where(['ghd_id' => $model->id])->andWhere(['teacher_id' => $teacher])->count()) {
                 $modelTHD = new TeacherHasDiscipline();
                 $modelTHD->teacher_id = $teacher;
                 $modelTHD->ghd_id = $model->id;
                 $modelTHD->save();
             }
         }
         $thd_old = TeacherHasDiscipline::find()->where(['ghd_id' => $model->id])->andWhere(['not in', 'teacher_id', Yii::$app->request->post()['GroupHasDiscipline']['teacherHasDiscipline']['teacher_id']])->all();
         foreach ($thd_old as $thd) {
             $thd->delete();
         }
         return $this->redirect(['manage']);
     } else {
         return $this->renderAjax('update', ['model' => $model]);
     }
 }