/**
  * @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();
     }
 }
Example #2
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;
 }