Looking for an easier way of doing multiple queries then appending each to an ActiveDataProvider

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


                    .....

            




You could use one data provider and set the "where" part of its query in the view.


$listDataProvider->query->andWhere(['book_type' => 'Short African Stories']);

Edit: Having a second look at your code, why not put the “Yii::$app->request->get(‘nav’)” part in your controller action?

Also: Consider using a separate table for your book types and store only a key in the upload table.

Thanks for the suggestions…I tried placing the querying the $listdataprovider from my view but it gives me the error Calling unknown method: yii\db\ActiveQuery::getCount()

1 Like

Please post the code that gives you the error.

As per your suggestion, I removed all the redundant dataproviders and I’m now sending one ActiveDataProvider to my view and trying to query from there

UploadsController





public function actionList()

    {


        $this->layout = 'main';

        $list_dataProvider = new ActiveDataProvider([

                   'query' => Uploads::find(),

                   'pagination' => [

                       'pageSize' => 8,

                   ],

               ]);


        return $this->render('list', 

            ['list' => $list_dataProvider]);

    }

List.php





//this line has produces the error in my view..

$listDataProvider = $list->query->andWhere(['book_type' => 'Bible Stories']);




.....


            <?=ListView::widget([

                'dataProvider' => $listDataProvider,

                'options' => [

                    'tag' => 'div',

                    'class' => 'list-group',

                    'id' => 'list-group',

                ],

                'layout' => "{items}\n{summary}\n{pager}",

.......




Try





$list->query->andWhere(['book_type' => 'Bible Stories']);




            <?=ListView::widget([

                'dataProvider' => $list,

                'options' => [

                    'tag' => 'div',

                    'class' => 'list-group',

                    'id' => 'list-group',

                ],

                'layout' => "{items}\n{summary}\n{pager}",




Works perfectly…Thanks