Sorting and Pagination does not work after I build a custom keyword search that is build using relational tables

I recently started to build a custom keyword search using Yii 1.1.x

The search works 100%. But as soon as I sort the columns and use pagination in the admin view the search gets lost and all results are shown. So with otherwords it’s not filtering so that only the search results show. Somehow it resets it.

In my controller my code looks as follows


 $builder=Messages::model()->getCommandBuilder();

    		

    		//Table1 Columns

    		$columns1=array('0'=>'id','1'=>'to','2'=>'from','3'=>'message','4'=>'error_code','5'=>'date_send');

    		

    		//Table 2 Columns

    		$columns2=array('0'=>'username');

    		

    		//building the Keywords

    		$keywords = explode(' ',$_REQUEST['search']);

    		$count=0;

    		foreach($keywords as $key){

    			$kw[$count]=$key;

    			++$count;

    		}	

    		

    		$keywords=$kw;

    	

    		$condition1=$builder->createSearchCondition(Messages::model()->tableName(),$columns1,$keywords,$prefix='t.');

    		$condition2=$builder->createSearchCondition(Users::model()->tableName(),$columns2,$keywords);

    		$condition = substr($condition1,0,-1) . " OR ".substr($condition2,1);

    		$condition = str_replace('AND','OR',$condition);

    

    

    $dataProvider=new CActiveDataProvider('Messages', array(

    				'pagination'=>array(

    					'pageSize'=>self::PAGE_SIZE,

    				),

    				'criteria'=>array(

    					'with'=>'users',

    					'together'=>true,

    					'joinType'=>'LEFT JOIN',

    					'condition'=>$condition,

    				),

    				

    				'sort'=>$sort,

    			));

    

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

    			'dataProvider'=>$dataProvider,'keywords'=>implode(' ',$keywords),'sort'=>$sort

    		));



and my view looks like this


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

    	'dataProvider'=>$dataProvider,

    	'columns'=>array(

    		'id',

    		array(

    			'name'=>'user_id',

    			'value'=>'CHtml::encode(Users::model()->getReseller($data->user_id))',

    			'visible'=>Yii::app()->user->checkAccess('poweradministrator')

    		),

    		'to',

    		'from',

    		'message',

    		/*

    		'date_send',

    		*/

    		array(

    			'name'=>'error_code',

    			'value'=>'CHtml::encode($data->status($data->error_code))',

    		),

    		array(

    			'class'=>'CButtonColumn',

    			'template'=>'{view} {delete}',

    		),

    		

    		

    	),

    	

    ));

I really do not know what do do anymore since I’m terribly lost, any help will be hihsly appreciated

Are you using AJAX? By default, the CGridView widget is in AJAX and it causes you to loose your search parameters in some cases (especially when navigating through the browser history).

You should try to disable it and see if your problem persists. In your view, make the search form visible, remove the javascript and add to your CGridView:




	'ajaxUpdate' => false



I attach a full view that has a trigger for AJAX. If the cause of your problem isn’t in this, good luck!

I’ll check this out, looks promising

Okey I managed to find out what the problem is here, it’s not sending the search values through to the next page using if I save the search values in a session the pagination and sorting works but this is not probably not the right way.

Maybe setData has something to do with this

eg.


$dataProvider=new CActiveDataProvider('Messages', array(

				'pagination'=>array(

					'pageSize'=>self::PAGE_SIZE,

				),

				'criteria'=>array(

					'condition'=>'user_id='.Yii::app()->user->u_id.' '.$condition,

				),

				'sort'=>$sort,

				

			));


$dataProvider->setData(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?);

I solved the problem by storing my criteria and sort objects in the session after they were set. Then, if the action method was called via ajax request, I reloaded them from the session before rendering the dataprovider. Here’s the forum post: http://www.yiiframework.com/forum/index.php?/topic/7695-how-to-maintain-criteria-filters-when-sorting-with-cgridview/. Good luck!

Thanx, I also did it this way and it works!!