No funciona paginación con CGridView

Hola,

El problema que tengo es con la vista ‘manage’ generada con el gii.

La vista está basada en el modelo de Clientes y este a su vez está relacionado con otros 3 modelos.

Me muestra los datos correctamente pero para que me siga funcionando la búsqueda he tenido que modificar el método search tal que así:




	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;

		$criteria->with=array('company','companyTT','persons');


		$criteria->compare('Id',$this->Id);

		$criteria->compare('company.Name',$this->NameOrg,true); 

		$criteria->compare('companyTT.Name',$this->NameClienteTT,true); 

		$criteria->compare('persons.Name',$this->PER,true); 


		$sort = new CSort();

		$sort->attributes = array(

			'defaultOrder'=>'NameOrg ASC',

			'NameOrg'=>array(

				'asc'=>'companyTT.Name',

				'desc'=>'companyTT.Name desc',

			),

			'NameClienteTT'=>array(

				'asc'=>'company.Name',

				'desc'=>'company.Name desc',

			),

			'PER'=>array(

				'asc'=>'persons.Name',

				'desc'=>'persons.Name desc',

			),																															

		);					


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		    'sort'=>$sort			

		));

	}



El caso es que me ha dejado de funcionar la paginación y no me carga los datos cuando intento cambiar de página. Investigando he descubierto que si quito esta línea del método search si que me funciona pero me deja de funcionar la búsqueda:


$criteria->with=array('company','companyTT','persons');

¿Alguna idea de qué puedes estar ocurriendo?

Gracias de antemano,

Hola Rubén,

¿Podría publicar el controlador y la vista también?

Creo que el problema está en uno de esos dos archivos.

Saludos

Hola,

Adjunto de nuevo el Modelo, la Vista y el Controlador:

Modelo:




	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;

		$criteria->with=array('organizaciones','organizacionesTT','contactos');


		$criteria->compare('Id',$this->Id);

		$criteria->compare('organizaciones.Nombre',$this->NombreOrg,true); 

		$criteria->compare('organizacionesTT.Nombre',$this->NombreClienteTT,true); 

		$criteria->compare('contactos.Nombre',$this->SDM,true); 


		$sort = new CSort();

		$sort->attributes = array(

			'defaultOrder'=>'NombreOrg ASC',

			'NombreOrg'=>array(

				'asc'=>'organizacionesTT.Nombre',

				'desc'=>'organizacionesTT.Nombre desc',

			),

			'NombreClienteTT'=>array(

				'asc'=>'organizaciones.Nombre',

				'desc'=>'organizaciones.Nombre desc',

			),

			'SDM'=>array(

				'asc'=>'contactos.Nombre',

				'desc'=>'contactos.Nombre desc',

			),																															

		);					


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		    'sort'=>$sort			

		));

	}



La Vista:




<?php

$this->breadcrumbs=array(

	'Servicios'=>array('/site/servicios'),

	'Clientes'=>array('index'),	

	'Listar',

);


$this->menu=array(

	array('label'=>'Create Clientes', 'url'=>array('create')),

);


Yii::app()->clientScript->registerScript('search', "

$('.search-button').click(function(){

	$('.search-form').toggle();

	return false;

});

$('.search-form form').submit(function(){

	$.fn.yiiGridView.update('clientes-grid', {

		data: $(this).serialize()

	});

	return false;

});

");

?>


<h1>Listar Clientes</h1>


<p>

You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>

or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.

</p>


<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'clientes-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		array (

			'name'=>'NombreOrg',

			'value'=>'$data->organizaciones->Nombre',

			'type'=>'text',

		),	

		array (

			'name'=>'NombreClienteTT',

			'value'=>'$data->organizacionesTT->Nombre',

			'type'=>'text',

		),				

		array (

			'name'=>'SDM',

			'value'=>'$data->contactos->Nombre',

			'type'=>'text',

		),		

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>



Y el Controlador:




<?php


class ClientesController extends Controller

{

	/**

	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning

	 * using two-column layout. See 'protected/views/layouts/column2.php'.

	 */

	public $layout='//layouts/column2';


	/**

	 * @return array action filters

	 */

	public function filters()

	{

		return array(

			'accessControl', // perform access control for CRUD operations

		);

	}


	/**

	 * Specifies the access control rules.

	 * This method is used by the 'accessControl' filter.

	 * @return array access control rules

	 */

	public function accessRules()

	{

		return array(

				

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view','exportexcel','exportpdf'),

				'users'=>array('@'),

			),		

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update'),

				'expression'=>'Yii::app()->session["rolseleccionado"] == "ADM" 

								OR Yii::app()->session["rolseleccionado"] == "OS"',

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

				'expression'=>'Yii::app()->session["rolseleccionado"] == "ADM"',

			),			

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}


	/**

	 * Displays a particular model.

	 * @param integer $id the ID of the model to be displayed

	 */

	public function actionView($id)

	{

		$this->render('view',array(

			'model'=>$this->loadModel($id),

		));

	}


	/**

	 * Creates a new model.

	 * If creation is successful, the browser will be redirected to the 'view' page.

	 */

	public function actionCreate()

	{

		$model=new Clientes;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Clientes']))

		{

			$model->attributes=$_POST['Clientes'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->Id));

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}


	/**

	 * Updates a particular model.

	 * If update is successful, the browser will be redirected to the 'view' page.

	 * @param integer $id the ID of the model to be updated

	 */

	public function actionUpdate($id)

	{

		$model=$this->loadModel($id);


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Clientes']))

		{

			$model->attributes=$_POST['Clientes'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->Id));

		}


		$this->render('update',array(

			'model'=>$model,

		));

	}


	/**

	 * Deletes a particular model.

	 * If deletion is successful, the browser will be redirected to the 'admin' page.

	 * @param integer $id the ID of the model to be deleted

	 */

	public function actionDelete($id)

	{

		if(Yii::app()->request->isPostRequest)

		{

			// we only allow deletion via POST request

			$this->loadModel($id)->delete();


			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

			if(!isset($_GET['ajax']))

				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

		}

		else

			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

	}


	/**

	 * Lists all models.

	 */

	public function actionIndex()

	{

		$model=new Clientes('search');

		$model->unsetAttributes();  // clear any default values

		if(isset($_GET['Clientes']))

			$model->attributes=$_GET['Clientes'];


		$this->render('index',array(

			'model'=>$model,

		));

	}




	/**

	 * Returns the data model based on the primary key given in the GET variable.

	 * If the data model is not found, an HTTP exception will be raised.

	 * @param integer the ID of the model to be loaded

	 */

	public function loadModel($id)

	{

		$model=Clientes::model()->findByPk($id);

		if($model===null)

			throw new CHttpException(404,'The requested page does not exist.');

		return $model;

	}


	/**

	 * Performs the AJAX validation.

	 * @param CModel the model to be validated

	 */

	protected function performAjaxValidation($model)

	{

		if(isset($_POST['ajax']) && $_POST['ajax']==='clientes-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}

	}

	

	/**

	 * ExportExcel

	 */

	public function actionExportExcel()

	{

		$model = new Clientes();

		$this->render('exportexcel',array(

			'model'=>$model,

		));

	}	

	

	/**

	 * ExportPDF

	 */

	public function actionExportPdf()

	{

		$model = new Clientes();

		$this->render('exportpdf',array(

			'model'=>$model,

		));

	}		

}




Gracias de antemano!