/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params, $query2 = null)
 {
     $query = TeacherHasDiscipline::find();
     // add conditions that should always apply here
     if ($query2) {
         $query = $query2;
     }
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $dataProvider->setSort(['attributes' => ['id', 'groupName' => ['asc' => ['groupHasDiscipline.group.name' => SORT_ASC], 'desc' => ['groupHasDiscipline.group.name' => SORT_DESC]], 'disciplineName' => ['asc' => ['groupHasDiscipline.discipline.name' => SORT_ASC], 'desc' => ['groupHasDiscipline.discipline.name' => SORT_DESC]], 'semester' => ['asc' => ['groupHasDiscipline.semester_number' => SORT_ASC], 'desc' => ['groupHasDiscipline.semester_number' => SORT_DESC]]]]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     // grid filtering conditions
     $query->andFilterWhere(['id' => $this->id, 'teacher_id' => $this->teacher_id, 'ghd_id' => $this->ghd_id, 'begin_date' => $this->begin_date, 'end_date' => $this->end_date]);
     $query->joinWith('groupHasDiscipline')->joinWith(['groupHasDiscipline.discipline' => function ($q) {
         $q->where('discipline.name LIKE "%' . $this->disciplineName . '%" ');
     }]);
     $query->joinWith('groupHasDiscipline')->joinWith(['groupHasDiscipline.group' => function ($q) {
         $q->where('group.name LIKE "%' . $this->groupName . '%" ');
     }]);
     $query->joinWith(['groupHasDiscipline' => function ($q) {
         $q->where('group_has_discipline.semester_number LIKE "%' . $this->semester . '%" ');
     }]);
     return $dataProvider;
 }
 public function getTeacherHasDiscipline()
 {
     return $this->hasMany(TeacherHasDiscipline::className(), ['ghd_id' => 'id']);
 }
 /**
  * Finds the TeacherHasDiscipline model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return TeacherHasDiscipline the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = TeacherHasDiscipline::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * 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]);
     }
 }