Code: Select all
private function actionPagination($query)
{
$count = $query->count();
$pagination = new Pagination([
'defaultPageSize' => 10,
'totalCount' => $count,
]);
$books = $query
->orderBy('id')
->offset($pagination->offset)
->limit($pagination->limit)
->all();
return [$books, $pagination];
}
public function actionIndex()
{
$query = Books::find();
list($books, $pagination) = $this->actionPagination($query);
return $this->render('index', [
'books' => $books,
'pagination' => $pagination,
'totalPages' => max(1, ceil($pagination->totalCount / $pagination->pageSize)),
]);
}
public function actionFilter()
{
$query = Books::find();
$genres = Genres::find()->select(['genre_name', 'id'])->orderBy('genre_name')->indexBy('id')->column();
$authors = Authors::find()->select(['concat(first_name, " ", last_name)', 'id'])->orderBy('id')->column();
$authors = array_combine(range(1, count($authors)), $authors);
$searchModel = new Books();
$requestParams = Yii::$app->request->queryParams;
if ($searchModel->load($requestParams)) {
if ($searchModel->year_filter) {
$query->andWhere(['year' => $searchModel->year_filter]);
}
if ($searchModel->genre_filter) {
$query->andWhere(['genre_id' => $searchModel->genre_filter]);
}
if ($searchModel->author_filter) {
$query->joinWith('links')
->andWhere(['links.author_id' => $searchModel->author_filter]);
}
}
return $this->render('index', [
'authors' => $authors,
'genres' => $genres,
'searchModel' => $searchModel,
]);
}
Code: Select all
Undefinierte Variable $searchModel
Ich möchte $searchModel erst erstellen, nachdem ich auf „Filtern“ geklickt habe.
Oder sollte ich verwenden Eine andere Methode?
Ich habe den Code ausprobiert, den ich aufgelistet habe.