CGridView and Pagination: Paging not possible if using old dataProvider

Hey guys,

i’m facing a problem making me crazy with CGridView (ok, original i’m using TbGridView, but CGridView has the same problem).

Short: I’m using a CArrayDataProvider stored in the user session. When doing a changeover, the table doesn’t refresh.

Doing like this:

Controller:




    protected function actionIndex()

    {

    	...

    	$dpProvider = $model->wsGetEntries(); //Fetching data from model, returning a CArrayDataProvider

    	Yii::app()->user->setState('dpProvider', $dpProvider );

	$this->redirect(array('showDetails'));

    	...

    }


...


    protected function actionShowDetails()

    {

    	...

    	$dpProvider = Yii::app()->user->getState('dpProvider');

        $this->render("showDetails", array('

                        'pageTitle'=>$pageTitle,

                        'dpProvider '=>$dpProvider ,

                        'model'=>$model )

        );

    }



View:




...

    	<?php $this->widget('bootstrap.widgets.TbGridView',array(

	                        'id'=>'ID',

	                        //'dataProvider'=>$dpProvider, // NOT WORKING

	                        'dataProvider'=>new CArrayDataProvider($dpProvider->rawData, // WORKING

    	    	    	    	    	    	    	    		array(

								    		'id'=>$dpProvider->getId(),

								    		'pagination' => $dpProvider->getPagination(),

								    		'sort'=>$dpProvider->getSort(),

    									)

    				),

	                        'pager'=>array(

	                            'class'=>'bootstrap.widgets.TbPager',

	                            'displayFirstAndLast'=>true,

	                            'htmlOptions'=>array('class'=>'pagination pagination-sm'),

	                        ),

	                        'htmlOptions'=>array('class'=>'g-grid-view'),

	                        'columns'=>array(...)));

    	?>

....



I already tried:




    protected function actionShowDetails()

    {

    	...

        //$currentPage somehow identified

        ...

    	$dpProvider = Yii::app()->user->getState('dpProvider');

        $dpProvider->getPagination()->setCurrentPage($currentPage)

        $this->render("showDetails", array('

                        'pageTitle'=>$pageTitle,

                        'dpProvider '=>$dpProvider ,

                        'model'=>$model )

        );

    }






...

    	<?php $this->widget('bootstrap.widgets.TbGridView',array(

	                        'id'=>'ID',

	                        'dataProvider'=>$dpProvider, // NOT WORKING

	                        'pager'=>array(

	                            'class'=>'bootstrap.widgets.TbPager',

	                            'displayFirstAndLast'=>true,

	                            'currentPage=$currentPage,

	                            'htmlOptions'=>array('class'=>'pagination pagination-sm'),

	                        ),

	                        'htmlOptions'=>array('class'=>'g-grid-view'),

	                        'columns'=>array(...)));

    	?>

....



And a couple of other ideas… but nothing worked really good, except from instantiating a new CArrayDataProvider.

But usually that’s against my expectations and my style of programming…

Anyone has an idea why it’s not working?

Best regards,

Schmu

It’s not a good idea to keep objects in session. First check if your data can be serialized because if not restored object might not be what you are expecting. Take a look at php magic methods __sleep and __wakeup.

Thank you, that was really the problem. I didn’t get the point why php can’t manage this, because obviously it’s working, but producing not reasonable errors.

Solved it for me by inheritating Yii Core class CWebUser.




	/**

	 * Returns the value of a variable that is stored in user session.

	 *

	 * This function is designed to be used by CWebUser descendant classes

	 * who want to store additional user information in user session.

	 * A variable, if stored in user session using {@link setState} can be

	 * retrieved back using this function.

	 *

	 * @param string $key variable name

	 * @param mixed $defaultValue default value

	 * @return mixed the value of the variable. If it doesn't exist in the session,

	 * the provided default value will be returned

	 * @see setState

	 */

	public function getState($key,$defaultValue=null)

	{

		$key=$this->getStateKeyPrefix().$key;

		$return = isset($_SESSION[$key]) ? $_SESSION[$key] : $defaultValue;

		if(substr($return, 0, 10) == '{isObject}' )

			$return = unserialize(substr($return, 10));

		return $return;

	}


	/**

	 * Stores a variable in user session.

	 *

	 * This function is designed to be used by CWebUser descendant classes

	 * who want to store additional user information in user session.

	 * By storing a variable using this function, the variable may be retrieved

	 * back later using {@link getState}. The variable will be persistent

	 * across page requests during a user session.

	 *

	 * @param string $key variable name

	 * @param mixed $value variable value

	 * @param mixed $defaultValue default value. If $value===$defaultValue, the variable will be

	 * removed from the session

	 * @see getState

	 */

	public function setState($key,$value,$defaultValue=null)

	{

		if(is_object($value))

			$value = '{isObject}'.serialize($value);

		if(is_object($defaultValue))

			$defaultValue = '{isObject}'.serialize($defaultValue);

		

		$key=$this->getStateKeyPrefix().$key;

		if($value===$defaultValue)

			unset($_SESSION[$key]);

		else

			$_SESSION[$key]=$value;

	}




That’s working fine.