public function generateComment() { $games = Game::find()->all(); foreach ($games as $game) { $comment = new CommentForm(); $comment->game_id = $game['id']; $comment->author_id = $this->user_id; $comment->title = $this->fake->text(32); $comment->content = $this->fake->realText(400); $comment->save(); } }
public function actionIndex() { //create sort object $sort = new Sort(['attributes' => ['id', 'title' => ['asc' => ['title' => SORT_ASC], 'desc' => ['title' => SORT_DESC], 'label' => 'Title'], 'created_at' => ['asc' => ['created_at' => SORT_ASC], 'desc' => ['created_at' => SORT_DESC], 'label' => 'Created at']]]); $sort->defaultOrder = ['created_at' => SORT_DESC]; //get all games $query = Game::find()->andFilterWhere(['like', 'title', Yii::$app->request->get('query')])->orFilterWhere(['like', 'content', Yii::$app->request->get('query')]); //get total number of games $count = $query->count(); //create pagination object $pagination = new Pagination(['totalCount' => $count]); //limit the query using the pagination and retrieve the game $games = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy($sort->orders)->all(); return $this->render('index', ['games' => $games, 'pagination' => $pagination, 'sort' => $sort]); }