Your Location is: Home > Php

Yii2 Pagination with LinkPager always returns first element

From: Nicaragua View: 3645 Valeria Salis 

Question

I'm working on a project which has a search form that might return many rows so I decided to manage that with Pagination (yii\data\Pagination) but in my view file, in pages different than first one, LinkPager always returns the maximum rows of result + the first one.

For example: if I put $pageSize = 1 in the first page it retrieves only one row but in second page it returns another row + the one in first page.

Here is my controller

public function actionVisualizzaAnnunci() {
$annunci1 = Annuncio::find()->where(['visibilita' => ['Pubblica', 'Privata']])->orderBy(['data_pubblicazione' => SORT_DESC]);

            $annunci_registrati = Annuncio::find()->where(['settore_id' => $settori_registrato])->orderBy(['data_pubblicazione' => SORT_DESC]);
           
            $result = $annunci1->union($annunci_registrati);

            $countQuery = clone $result;
            
            $pagination = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 1]);           
            $annunci = $result->offset($pagination->offset)
                    ->limit($pagination->limit)
                    ->all();
        }




        return $this->render('visualizza-annunci', ['annunci' => $annunci, 'pagination' => $pagination]);
}

Here is my view file

                    foreach ($annunci as $annuncio) {
                        ?>
                         <div class="risultato-ricerca">
                    <!--box ha link verso l'annuncio-->
                     <a href="dettaglio-annuncio?id=<?php echo $annuncio->id;?>">  
                       <div class="row">
                            <div class="img-annuncio col-2">
                                <img src="<?php echo Yii::$app->homeUrl; ?>/images/icon.png" class="img-annuncio img-fluid">
                            </div>
                            <div class="annuncio col-8">
                                <h3>     <?php echo $annuncio->titolo; ?></h3>
                                <h5> <?php echo $annuncio->azienda->citta; ?> | <?php echo $annuncio->data_pubblicazione; ?></h5>
                                <p><?php echo $annuncio->descrizione; ?></p>
                                <p class="right"> <?php echo $annuncio->tipo_contratto; ?> | <?php echo $annuncio->retribuzione_lorda; ?></p>
                            </div>
                        </div>
                    </a>
                </div>
   
               <?php     }  ?>

<?php 
        echo LinkPager::widget([
    'pagination' => $pagination,
]);
        
        ?>

Can someone help me?

Best answer

This issue happen since you are using UNION, so that result will be change in each page dependence of both query...

To resolve this issue:

  1. dependence of your query, you can do join(on the same table with alais) or add another condition to retrieve the same data....(This point mean, you can write your query in another way which its return expected result.) - if thats cant done for some reason you can go to second point...
  2. when you use union, you can use sqlDataProvider, for example:
$dataProvider = new SqlDataProvider([
    'sql' => $result,// $annunci1->union($annunci_registrati);
  ]);
  1. Also you can try to use custom query command and handle it manually.