Yii pagination shows fewer items than is available

I have a simple CGridView that loads from CActiveDataProvider. At the moment, and I'm not sure how long this has been happening, it does not display all the data items in the view with pagenation enabled.

My heading displays "Showing 1-7 of 9 results", but there are no buttons for more pages. If I set the pageSize value of the pagenation property of the data provider to a small number, I end up with search buttons, but it looks like there will be fewer elements on the first page than on the second page. For example, if I set pageSize for CActiveDataProvider to 3, I get 2.2.3 (positions on each page) instead of 3.3.1, as I would expect.

If I set pageSize to anything between 9 and 11 inclusive, there are elements that I don’t see because I get only one page, and not all elements are displayed ("1-6 of 9" if I set pageSize to 9 )

$criteria=new CDbCriteria; $criteria->with = array('registrations'); $criteria->addCondition('registrations.id IS NOT NULL'); $criteria->addCondition('registered = false'); $criteria->together = true; $dataProvider = new CActiveDataProvider('Skier', array('criteria'=>$criteria)); $this->widget('zii.widgets.grid.CGridView', array( 'dataProvider'=>$dataProvider, 'columns'=>array( array('name'=>'fullname', 'header'=>'Name'), array( 'name'=>'programs_names', 'header'=>'Programs', 'value'=>'$data->programs_names', ), <More items here> ) )); 

Can anyone think what might make pagenation so fun?

Change Also, changing CActiveDataProvider to CArrayDataProvider works correctly, and I get 9 out of 9 results. This will work now because I have small data sets, but I would rather find out what the problem is.

 $dataProvider = new CArrayDataProvider(Skier::model()->findAll($criteria)); 
+6
source share
5 answers

If you are using complex query try this

 $count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM tbl_user')->queryScalar(); $sql='SELECT * FROM tbl_user'; $dataProvider=new CSqlDataProvider($sql, array( 'totalItemCount'=>$count, 'sort'=>array( 'attributes'=>array( 'id', 'username', 'email', ), ), 'pagination'=>array( 'pageSize'=>10, ), )); // $dataProvider->getData() will return a list of arrays. 

Then you can pass your $ dataProvider to your CGridView

Further documents on using a custom query as a dataProvider, check here.

+1
source

I had such a problem. this was due to duplication of records (primary keys) in the database.

+1
source

I had to put $criteria->with and its together=true inside if() :

 if( (int) $this->categoria_id > 0) { $criteria->with = Array ( 'categorie'=> Array ( 'together' => true, ) ); $criteria->compare ('categorie.id', $this->categoria_id); } 

otherwise CActiveDataProvider pagination with together=true and no parameter passed returns an invalid counter

+1
source

To display exactly ALL records, 'count rows' and then 'put $ countResult on pageSize' , for example

First, count lines like (this is an example, "User" = "your model name"))

 $countResult = User::model()->countByAttributes(array('record_name' => $matched_record_name)); 

OR

 $countResult = User::model()->count($condition); 

And then put $ countResult on pageSize

  'pagination'=>array( 'pageSize' => intval($countResult), )), 
0
source

Try adding the following to your Skier model search method:

 $criteria->group = "id"; 

Or:

 $criteria->group = "t.id"; 
0
source

Source: https://habr.com/ru/post/959357/


All Articles