diff --git a/config/db.php b/config/db.php index c4c1252..42106e5 100644 --- a/config/db.php +++ b/config/db.php @@ -2,8 +2,8 @@ return [ 'class' => 'yii\db\Connection', - 'dsn' => 'mysql:host=localhost;dbname=yii2basic', + 'dsn' => 'mysql:host=localhost;dbname=univer_yii', 'username' => 'root', - 'password' => '', + 'password' => 'qqq', 'charset' => 'utf8', ]; diff --git a/config/web.php b/config/web.php index 9a23983..34f49cc 100644 --- a/config/web.php +++ b/config/web.php @@ -4,6 +4,7 @@ $config = [ 'id' => 'basic', + 'language' => 'ru-RU', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], 'components' => [ @@ -45,6 +46,13 @@ ], ], ], + // my module for home work ln8 geekhub phpAdvance + // module: create - crud, db - migrations + 'modules' => [ + 'homework' => [ + 'class' => 'app\modules\homework\Module', + ], + ], 'params' => $params, ]; diff --git a/controllers/AppController.php b/controllers/AppController.php new file mode 100644 index 0000000..f61946f --- /dev/null +++ b/controllers/AppController.php @@ -0,0 +1,10 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all Department models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new DepartmentSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Department model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new Department model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new Department(); + + 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 University 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()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } + + /** + * Deletes an existing University model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the University model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return Department the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = Department::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} \ No newline at end of file diff --git a/controllers/StudentController.php b/controllers/StudentController.php new file mode 100644 index 0000000..0c8c3cd --- /dev/null +++ b/controllers/StudentController.php @@ -0,0 +1,120 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all Student models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new StudentSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Student model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new Student model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new Student(); + + 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 Student 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()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } + + /** + * Deletes an existing Student model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the Student model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return Student the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = Student::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} \ No newline at end of file diff --git a/controllers/TeacherController.php b/controllers/TeacherController.php new file mode 100644 index 0000000..942df6c --- /dev/null +++ b/controllers/TeacherController.php @@ -0,0 +1,120 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all Teacher models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new TeacherSearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single Teacher model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new Teacher model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new Teacher(); + + 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 Teacher 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()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } + + /** + * Deletes an existing Teacher model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the Teacher model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return Teacher the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = Teacher::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} \ No newline at end of file diff --git a/controllers/UniversityController.php b/controllers/UniversityController.php new file mode 100644 index 0000000..e506225 --- /dev/null +++ b/controllers/UniversityController.php @@ -0,0 +1,120 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all University models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new UniversitySearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single University model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new University model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new University(); + + 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 University 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()) { + return $this->redirect(['view', 'id' => $model->id]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } + + /** + * Deletes an existing University model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * @param integer $id + * @return mixed + */ + public function actionDelete($id) + { + $this->findModel($id)->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the University model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return University the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = University::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} \ No newline at end of file diff --git a/migrations/m161203_155259_create_university_table.php b/migrations/m161203_155259_create_university_table.php new file mode 100644 index 0000000..96a4d0b --- /dev/null +++ b/migrations/m161203_155259_create_university_table.php @@ -0,0 +1,27 @@ +createTable('university', [ + 'id' => $this->primaryKey(), + ]); + } + + /** + * @inheritdoc + */ + public function down() + { + $this->dropTable('university'); + } +} diff --git a/migrations/m161203_155345_add_university_name_column_to_university_table.php b/migrations/m161203_155345_add_university_name_column_to_university_table.php new file mode 100644 index 0000000..fe2008a --- /dev/null +++ b/migrations/m161203_155345_add_university_name_column_to_university_table.php @@ -0,0 +1,25 @@ +addColumn('university', 'university_name', $this->string()); + } + + /** + * @inheritdoc + */ + public function down() + { + $this->dropColumn('university', 'university_name'); + } +} diff --git a/migrations/m161203_155421_create_department_table.php b/migrations/m161203_155421_create_department_table.php new file mode 100644 index 0000000..8344272 --- /dev/null +++ b/migrations/m161203_155421_create_department_table.php @@ -0,0 +1,61 @@ +createTable('department', [ + 'id' => $this->primaryKey(), + 'university_id' => $this->integer()->notNull(), + 'department_name' => $this->string(), + ]); + + // creates index for column `university_id` + $this->createIndex( + 'idx-department-university_id', + 'department', + 'university_id' + ); + + // add foreign key for table `university` + $this->addForeignKey( + 'fk-department-university_id', + 'department', + 'university_id', + 'university', + 'id', + 'CASCADE' + ); + } + + /** + * @inheritdoc + */ + public function down() + { + // drops foreign key for table `university` + $this->dropForeignKey( + 'fk-department-university_id', + 'department' + ); + + // drops index for column `university_id` + $this->dropIndex( + 'idx-department-university_id', + 'department' + ); + + $this->dropTable('department'); + } +} diff --git a/migrations/m161204_201453_create_teacher_table.php b/migrations/m161204_201453_create_teacher_table.php new file mode 100644 index 0000000..3b225be --- /dev/null +++ b/migrations/m161204_201453_create_teacher_table.php @@ -0,0 +1,28 @@ +createTable('teacher', [ + 'id' => $this->primaryKey(), + 'teacher_name' => $this->string(80) + ]); + } + + /** + * @inheritdoc + */ + public function down() + { + $this->dropTable('teacher'); + } +} diff --git a/migrations/m161204_201645_create_student_table.php b/migrations/m161204_201645_create_student_table.php new file mode 100644 index 0000000..f5da182 --- /dev/null +++ b/migrations/m161204_201645_create_student_table.php @@ -0,0 +1,28 @@ +createTable('student', [ + 'id' => $this->primaryKey(), + 'student_name' => $this->string(80) + ]); + } + + /** + * @inheritdoc + */ + public function down() + { + $this->dropTable('student'); + } +} diff --git a/migrations/m161204_202545_create_junction_table_for_teacher_and_student_tables.php b/migrations/m161204_202545_create_junction_table_for_teacher_and_student_tables.php new file mode 100644 index 0000000..36a538b --- /dev/null +++ b/migrations/m161204_202545_create_junction_table_for_teacher_and_student_tables.php @@ -0,0 +1,91 @@ +createTable('teacher_student', [ + 'teacher_id' => $this->integer(), + 'student_id' => $this->integer(), + 'PRIMARY KEY(teacher_id, student_id)', + ]); + + // creates index for column `teacher_id` + $this->createIndex( + 'idx-teacher_student-teacher_id', + 'teacher_student', + 'teacher_id' + ); + + // add foreign key for table `teacher` + $this->addForeignKey( + 'fk-teacher_student-teacher_id', + 'teacher_student', + 'teacher_id', + 'teacher', + 'id', + 'CASCADE' + ); + + // creates index for column `student_id` + $this->createIndex( + 'idx-teacher_student-student_id', + 'teacher_student', + 'student_id' + ); + + // add foreign key for table `student` + $this->addForeignKey( + 'fk-teacher_student-student_id', + 'teacher_student', + 'student_id', + 'student', + 'id', + 'CASCADE' + ); + } + + /** + * @inheritdoc + */ + public function down() + { + // drops foreign key for table `teacher` + $this->dropForeignKey( + 'fk-teacher_student-teacher_id', + 'teacher_student' + ); + + // drops index for column `teacher_id` + $this->dropIndex( + 'idx-teacher_student-teacher_id', + 'teacher_student' + ); + + // drops foreign key for table `student` + $this->dropForeignKey( + 'fk-teacher_student-student_id', + 'teacher_student' + ); + + // drops index for column `student_id` + $this->dropIndex( + 'idx-teacher_student-student_id', + 'teacher_student' + ); + + $this->dropTable('teacher_student'); + } +} diff --git a/models/Department.php b/models/Department.php new file mode 100644 index 0000000..a9f283d --- /dev/null +++ b/models/Department.php @@ -0,0 +1,59 @@ + 'ID', + 'department_name' => 'Department Name', + 'university_id' => 'University Id', + ]; + } + + /** + * relation Many-to-One: many Department, One Univer + * @return \yii\db\ActiveQuery + */ + public function getUniversity() + { + return $this->hasOne(University::className(), ['id' => 'university_id']); + } +} \ No newline at end of file diff --git a/models/DepartmentSearch.php b/models/DepartmentSearch.php new file mode 100644 index 0000000..c3f8378 --- /dev/null +++ b/models/DepartmentSearch.php @@ -0,0 +1,67 @@ + $query, + ]); + + $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, + ]); + + $query->andFilterWhere(['like', 'department_name', $this->department_name]) + ->andFilterWhere(['like', 'university_id', $this->university_id]); + + return $dataProvider; + } +} \ No newline at end of file diff --git a/models/Student.php b/models/Student.php new file mode 100644 index 0000000..ddbaaa5 --- /dev/null +++ b/models/Student.php @@ -0,0 +1,58 @@ + 'ID', + 'student_name' => 'Student Name', + ]; + } + + /** + * relation Many-to-Many: many Student - many Teacher + * @return \yii\db\ActiveQuery + */ + public function getTeachers() + { + return $this->hasMany(Teacher::className(), ['id' => 'teacher_id']) + ->viaTable('teacher_student', ['student_id' => 'id']); + } +} \ No newline at end of file diff --git a/models/StudentSearch.php b/models/StudentSearch.php new file mode 100644 index 0000000..bba164c --- /dev/null +++ b/models/StudentSearch.php @@ -0,0 +1,66 @@ + $query, + ]); + + $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, + ]); + + $query->andFilterWhere(['like', 'student_name', $this->student_name]); + + return $dataProvider; + } +} \ No newline at end of file diff --git a/models/Teacher.php b/models/Teacher.php new file mode 100644 index 0000000..6172faa --- /dev/null +++ b/models/Teacher.php @@ -0,0 +1,58 @@ + 'ID', + 'teacher_name' => 'Teacher Name', + ]; + } + + /** + * relation Many-to-Many: many Student - many Teacher + * @return \yii\db\ActiveQuery + */ + public function getStudents() + { + return $this->hasMany(Student::className(), ['id' => 'student_id']) + ->viaTable('teacher_student', ['teacher_id' => 'id']); + } +} \ No newline at end of file diff --git a/models/TeacherSearch.php b/models/TeacherSearch.php new file mode 100644 index 0000000..d34d1a6 --- /dev/null +++ b/models/TeacherSearch.php @@ -0,0 +1,66 @@ + $query, + ]); + + $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, + ]); + + $query->andFilterWhere(['like', 'teacher_name', $this->teacher_name]); + + return $dataProvider; + } +} \ No newline at end of file diff --git a/models/University.php b/models/University.php new file mode 100644 index 0000000..1c62fc4 --- /dev/null +++ b/models/University.php @@ -0,0 +1,57 @@ + 'ID', + 'university_name' => 'University Name', + ]; + } + + /** + * relation One-to-Many: One University - many Department + * @return \yii\db\ActiveQuery + */ + public function getDepartment() + { + return $this->hasMany(Department::className(), ['university_id' => 'id']); + } +} \ No newline at end of file diff --git a/models/UniversitySearch.php b/models/UniversitySearch.php new file mode 100644 index 0000000..3083396 --- /dev/null +++ b/models/UniversitySearch.php @@ -0,0 +1,66 @@ + $query, + ]); + + $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, + ]); + + $query->andFilterWhere(['like', 'university_name', $this->university_name]); + + return $dataProvider; + } +} \ No newline at end of file diff --git a/views/department/_form.php b/views/department/_form.php new file mode 100644 index 0000000..57bf153 --- /dev/null +++ b/views/department/_form.php @@ -0,0 +1,26 @@ + + +
+ = Html::a('Create Department', ['create'], ['class' => 'btn btn-success']) ?> +
+ = GridView::widget([ + 'dataProvider' => $dataProvider, + 'filterModel' => $searchModel, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + + 'id', + 'department_name', + 'university_id', + + ['class' => 'yii\grid\ActionColumn'], + ], + ]); ?> ++ = Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?> + = Html::a('Delete', ['delete', 'id' => $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +
+ + = DetailView::widget([ + 'model' => $model, + 'attributes' => [ + 'id', + 'university_id', + 'department_name', + ], + ]) ?> + ++ = Html::a('Create Student', ['create'], ['class' => 'btn btn-success']) ?> +
+ = GridView::widget([ + 'dataProvider' => $dataProvider, + 'filterModel' => $searchModel, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + + 'id', + 'student_name', + + ['class' => 'yii\grid\ActionColumn'], + ], + ]); ?> ++ = Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?> + = Html::a('Delete', ['delete', 'id' => $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +
+ + = DetailView::widget([ + 'model' => $model, + 'attributes' => [ + 'id', + 'student_name', + ], + ]) ?> + ++ = Html::a('Create Teacher', ['create'], ['class' => 'btn btn-success']) ?> +
+ = GridView::widget([ + 'dataProvider' => $dataProvider, + 'filterModel' => $searchModel, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + + 'id', + 'teacher_name', + + ['class' => 'yii\grid\ActionColumn'], + ], + ]); ?> ++ = Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?> + = Html::a('Delete', ['delete', 'id' => $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +
+ + = DetailView::widget([ + 'model' => $model, + 'attributes' => [ + 'id', + 'teacher_name', + ], + ]) ?> + ++ = Html::a('Create University', ['create'], ['class' => 'btn btn-success']) ?> +
+ = GridView::widget([ + 'dataProvider' => $dataProvider, + 'filterModel' => $searchModel, + 'columns' => [ + ['class' => 'yii\grid\SerialColumn'], + + 'id', + 'university_name', + + ['class' => 'yii\grid\ActionColumn'], + ], + ]); ?> ++ = Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?> + = Html::a('Delete', ['delete', 'id' => $model->id], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => 'Are you sure you want to delete this item?', + 'method' => 'post', + ], + ]) ?> +
+ + = DetailView::widget([ + 'model' => $model, + 'attributes' => [ + 'id', + 'university_name', + ], + ]) ?> + +