An issue with Yii2 pagination(showing fewer records than should)

I have a problem with Yii2 pagination. I use ListView widget to show data. Also I am using SetSort in my model to sort data. My search model code:


public function search($params)

    {

        $query = Items::find();

        $dataProvider = new ActiveDataProvider([

            'query' => $query,

        ]);

        $dataProvider->setSort([

            'defaultOrder' => ['position' => SORT_ASC],

            'attributes' => [

                'position' => [

                    'asc' => ['items.is_top'=>SORT_ASC,'items.position' => SORT_DESC],

                    'label' => 'By popularity',

                ],

                'pricerup' => [

                    'asc' => ['prices.price' => SORT_ASC],

                    'label' => 'Exprensive to cheap'

                ],

                'pricerdown' => [

                    'asc' => ['prices.price' => SORT_DESC],

                    'label' => 'Cheap to expensive'

                ],

            ]

        ]); 

        if (!($this->load($params) && $this->validate())) {

            $query->joinWith(['prices']);

            return $dataProvider;

        }

        $query->andFilterWhere([

            'discount' => $this->discount,

            'present_id' => $this->present_id,

            'is_top' => $this->is_top,

            'manufacturer_id' => $this->manufacturer_id,

        ]);


        $query->andFilterWhere(['like', 'name', $this->name])

            ->andFilterWhere(['like', 'description', $this->description])

            ->andFilterWhere(['like', 'taste', $this->taste])

            ->andFilterWhere(['like', 'country', $this->country])

            ->andFilterWhere(['like', 'slug', $this->slug])

            ->andFilterWhere(['like', 'short_desc', $this->short_desc])

            ->andFilterWhere(['like', 'thumbnail', $this->thumbnail]);


        return $dataProvider;

    }

I know that this is not a very good decision to divide DESC and ASC logic for price, but this is needed for current design. My controller code:


public function actionIndex()

    {

        $searchModel = new ItemsSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());

        return $this->render('index', [

            'dataProvider'=>$dataProvider,

            'searchModel'=>$searchModel,

        ]);

    }

As I said, in view I use ListView. This is my view code:


<? Pjax::begin(['id' => 'items', 'clientOptions' => ['method' => 'POST']]);?>

                               <?

                               $dataProvider->pagination = [

                                   'defaultPageSize' => 8,

                               ];

                               echo ListView::widget( [

                                   'id'=>'items',

                                   'pager'        => [


                                       'firstPageLabel'    => "",

                                       'disabledPageCssClass'=>'swiper-button-disabled',

                                       'lastPageLabel'     => "",

                                       'nextPageLabel'     => ">",

                                       'prevPageLabel'     => "<",

                                       'maxButtonCount'=>0,

                                       'nextPageCssClass'=>'total-button-next',                                     'prevPageCssClass'=>'total-button-prev',

                                       'options'=>['id'=>'poplinks','class'=>'col-md-12 total-slider-orders margin-right-null padding-left-right-yes total-down-arrow']


                                   ],

                                'dataProvider' => $dataProvider,

                                'itemView' => '_item',

                                   'summary'=>'',


                               ] );

                               Pjax::end();?>

As a result I get something like this:

6640

Screenshot from 2015-07-26 17:08:55.png

As you can see, it doesn’t have last element on the first page, on the next pages everything is ok. I think that this is something with the offset,limit. Can someone tell me, where is an error? Thanks!

Hi f1r3starter, welcome to the forum.

It looks like one of the frequently observed issues of has many.

Probably the query is fetching 8 rows, but some of them share the same id of ‘Item’ … the resulting array of AR contains just 7 items, with one instance containing 2 prices.

The workaround may be using ‘Price’ AR model instead of ‘Item’, assuming ‘price’ has one ‘item’.

Hi softark, thank you for reply.

I understand now, what is the problem.

What about search model? Should I rebuild Prices search model for sorting and filtering data of Items or there is any way to use existing Items search model?