This is my first post so kindly go easy on me. I have the following code and I was looking for an easier way because while what I’ve done works, It just feels brute-forced.
I have a model class Uploads(gii-generated) that am querying data from and saving results in say variable $african which I then place in ActiveDataProvider. I finally send all my dataproviders to view called list that places the required provider in a listview widget.
UploadsController
public function actionList()
{
$this->layout = 'main';
$african = Uploads::find()
->where(['book_type' => 'Short African Stories'])
->orderBy('book_id');
$arabian = Uploads::find()
->where(['book_type' => 'Arabian ForkTales'])
->orderBy('book_id');
$bible = Uploads::find()
->where(['book_type' => 'Bible Stories'])
->orderBy('book_id');
$fairy = Uploads::find()
->where(['book_type' => 'Fairy Tales'])
->orderBy('book_id');
$fairy_dataProvider = new ActiveDataProvider([
'query' => $fairy,
'pagination' => [
'pageSize' => 8,
],
]);
$bible_dataProvider = new ActiveDataProvider([
'query' => $bible,
'pagination' => [
'pageSize' => 8,
],
]);
$african_dataProvider = new ActiveDataProvider([
'query' => $african,
'pagination' => [
'pageSize' => 8,
],
]);
$arabian_dataProvider = new ActiveDataProvider([
'query' => $arabian,
'pagination' => [
'pageSize' => 8,
],
]);
return $this->render('list',
['fairy_list' => $fairy_dataProvider,
'bible_list' => $bible_dataProvider,
'african_list' => $african_dataProvider,
'arabian_list' => $arabian_dataProvider]);
}
List.php
.....
if (Yii::$app->request->get('nav') == 'Arabian')
{
$content_title = 'Arabian Forktales';
$listDataProvider = $arabian_list;
}
else if (Yii::$app->request->get('nav') == 'African')
{
$content_title = 'Short African Stories';
$listDataProvider = $african_list;
}....
<?=ListView::widget([
'dataProvider' => $listDataProvider,
'options' => [
'tag' => 'div',
'class' => 'list-group',
'id' => 'list-group',
],
'layout' => "{items}\n{summary}\n{pager}",
'itemView' => function ($model, $key, $index, $widget) {
return $this->render('item_list',['model' => $model
.....