CGridView pagination problem

After searching the forum and not finding a solution, I need your help, I have a model that is related to another one and when trying to show the data, pagination does not work, when clicking on the following it keeps showing the same records, the strange thing is that If I order by some column I can use the page without problems.

my view

 <?php $this->widget('zii.widgets.grid.CGridView', array(
 	'id'=>'unidad-grid',
 	'itemsCssClass'=>'table table-hover table-striped table-bordered table-condensed',
 	'dataProvider'=>$model->search(),
 	'filter'=>$model,
 	'columns'=>array(
 		'UniCod',
 		'UniNom',
 		array(
 		 	'name'=>'EdiNom',
             'value' => '$data->ediCod->EdiNom',
         ),
 		'UniEst',
 		// 'UniVal',
 		
 	),
 )); ?>

my model:
relation:

  public function relations()
     {
         // NOTE: you may need to adjust the relation name and the related
         // class name for the relations automatically generated below.
         return array(
             'ediCod' => array(self::BELONGS_TO, 'Edificio', 'EdiCod'),
             // 'reservas' => array(self::HAS_MANY, 'Reserva', 'UniCod'),
         );
     }

search:

 public function search()
 	{
 
 		$criteria=new CDbCriteria;
 		$criteria->with = array('ediCod');
 		$criteria->compare('UniCod',$this->UniCod,true);
 		$criteria->compare('UniNom',$this->UniNom,true);
 		$criteria->compare('UniEst',$this->UniEst,true);
 		$criteria->compare('ediCod.EdiNom',$this->EdiNom,true);
 	  	$criteria->group = 'UniCod,UniNom,UniEst,UniVal,t.EdiCod,ediCod.EdiCod,ediCod.EdiNom,ediCod.SecCod';
         $criteria->together = true;
 
 		$sort = new CSort();
 		$sort->defaultOrder = array( 'UniCod DESC');
 		$sort->attributes = array(
 			'EdiNom'=>array(
 			    'asc'=>'ediCod.EdiNom',
 			    'desc'=>'ediCod.EdiNom desc',
 			),
 			'*',
 		);
 
 		 $dp = new CActiveDataProvider(Unidad::model(), array(
 	  		'criteria'=>$criteria,
             // 'Pagination'=>FALSE,
 	  		'sort'=>$sort,
 	  		// 'pagination'=>array(
 			  //     'pageSize'=>5,
 			  //   ),
 	  		));
 		 $dp->setTotalItemCount(count(Unidad::model()->findAll($criteria)));
 		 var_dump(count(Unidad::model()->findAll($criteria)));
 	  return $dp;
 	}

Does the JOIN produce unique rows or are you getting duplicate entries?

You may need to add a distinct option to the query.

https://www.yiiframework.com/doc/api/1.1/CDbCriteria#distinct-detail

thank you for the answer, the count give the correct numbers of rows, i try with defaultOrder in sort and works fine but the sort not work in any column from page two in pagination.

This looks suspicious to me. The dataprovider should be able to figure out the correct number of items itself.

What happens if you remove these two lines and add distinct to the query?

I took those commands from another forum with the same problem, for now I can not do the test in the afternoon I can barely try what you say, thanks for the reply