/**
  * Creates a new StudentTeacher model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new StudentTeacher();
     $post = Yii::$app->request->post();
     if (isset($post['StudentTeacher'])) {
         $studentId = $post['StudentTeacher']['student_id'];
         $teacherId = $post['StudentTeacher']['teacher_id'];
         $exists = StudentTeacher::find()->andWhere(['student_id' => $studentId])->andWhere(['teacher_id' => $teacherId])->exists();
         if (true === $exists) {
             return $this->render('create', ['model' => $model, 'message' => 'Этут студент уже учится у этого учителя']);
         }
     }
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->render('create', ['model' => $model, 'message' => 'Ученик успешно назначен учителю']);
     } else {
         return $this->render('create', ['model' => $model, 'message' => '']);
     }
 }
예제 #2
0
 /**
  * @throws \yii\db\Exception
  */
 private function generateLinks()
 {
     $c = Yii::$app->db->createCommand();
     $newBatch = [];
     $currentTeacherId = $this->minTeacherId;
     do {
         $studentsNum = rand(0, $this->getConfig('max_teacher_students'));
         if (0 == $studentsNum) {
             continue;
         }
         $batch = [];
         while (count($batch) < $studentsNum) {
             $batch[rand($this->minStudentId, $this->maxStudentId)] = true;
         }
         foreach ($batch as $k => $_) {
             $newBatch[] = [$k, $currentTeacherId];
         }
         if (count($newBatch) >= $this->getConfig('insert_portion_size')) {
             $c->batchInsert(StudentTeacher::tableName(), ['student_id', 'teacher_id'], $newBatch);
             $c->execute();
             $newBatch = [];
         }
         $currentTeacherId++;
     } while ($currentTeacherId <= $this->maxTeacherId);
     if (!empty($newBatch)) {
         $c->batchInsert(StudentTeacher::tableName(), ['student_id', 'teacher_id'], $newBatch);
         $c->execute();
     }
 }
예제 #3
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getListWithStudentsCounrt()
 {
     $query = Teacher::find()->joinWith(StudentTeacher::tableName())->addSelect([Teacher::tableName() . '.*', 'COUNT(' . StudentTeacher::tableName() . '.student_id) as student_count'])->groupBy(Teacher::tableName() . '.id');
     return $query;
 }