public function search($params) { $query = Affiliation::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'active' => $this->active, 'student_id' => $this->student_id, 'school_id' => $this->school_id, 'role_id' => $this->role_id, 'hand_anchor_id' => $this->hand_anchor_id, 'weapon_anchor_id' => $this->weapon_anchor_id, 'start_date' => $this->start_date]); return $dataProvider; }
/** * Creates a new Student model. * If creation is successful, the browser will be redirected to the either * the index or the create page to add another. * @return mixed */ public function actionCreate() { $transaction = Yii::$app->db->beginTransaction(); // Get the user's school and the current date. $schoolId = Yii::$app->user->schoolId; $today = date('Y-m-d'); $student = new Student(); $student->contract_school_id = $schoolId; $student->start_date = $today; if ($student->load(Yii::$app->request->post()) && $student->save()) { // Affiliate the new student with the same school as the user $role = Role::find()->where(['like', 'name', 'student'])->one(); $affiliation = new Affiliation(); if ($affiliation->load(Yii::$app->request->post())) { $affiliation->student_id = $student->id; $affiliation->school_id = $schoolId; $affiliation->role_id = $role->id; $affiliation->date = $today; $affiliation->save(); } /* Get the phone # that was posted along with the new student, * and assign it to the student via the newly-created ID. */ $phone = new StudentPhone(); if ($phone->load(Yii::$app->request->post())) { $phone->student_id = $student->id; $phone->save(); } if ($affiliation->isSaved && $phone->isSaved) { $transaction->commit(); $url = Html::a($student->name, ['update', 'id' => $student->id]); Yii::$app->session->setFlash('success', 'Student added: ' . $url); if (Yii::$app->request->post('then-add') !== null) { return $this->redirect(['create']); } elseif (Yii::$app->request->post('then-edit') !== null) { return $this->redirect(['update', 'id' => $student->id]); } else { return $this->redirect(['index']); } } else { $transaction->rollBack(); $msg = 'Unable to create student: '; //!!! find a better way to extract the model errors $msg .= serialize($affiliation->errors); $msg .= serialize($phone->errors); Yii::$app->session->setFlash('error', $msg); } } return $this->render('create', ['model' => $student]); }
/** * @return \yii\db\ActiveQuery relation */ public function getAffiliations() { return $this->hasMany(Affiliation::className(), ['role_id' => 'id']); }
/** * Finds the Affiliation model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Affiliation the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Affiliation::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }