Store CGridView state (CSort, CPagination and CDbCriteria)

I just created all the CRUD operation using gii. To make the CGridView a bit more user friendly I like to store the CPagination, CSort and CDbCriteria objects in a session or cookie so when the user comes back to the page, I can restore the CGridView is in the same state as when the user left the page.

When I try the catch the object in the action search() is doesn’t get the new CSort, CPagination or CDbCriteria parameters after sorting, filtering or navigating the CGridView.

    public function search() {

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

        // should not be searched.

        $criteria = new CDbCriteria;

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

        $criteria->compare('country', $this->country, true);

        $criteria->compare('province', $this->province, true);

        $criteria->compare('municipality', $this->municipality, true);

        $criteria->compare('city', $this->city, true);

        $criteria->compare('deeplink', $this->deeplink, true);

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

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

        $criteria->with = array('country');

        $dp = new CActiveDataProvider(get_class($this), array(

            'criteria' => $criteria,





            'sort' => array(

                'defaultOrder' => 'id_city',

                'attributes' => array(


                    'country' => array(

                        'asc' => '',

                        'desc' => ' DESC',






        //save page, order, filter

        $par['pagination'] = $dp->getPagination();

        $par['criteria'] = $dp->getCriteria();

        $par['sort'] = $dp->getSort();

        $cookie = new CHttpCookie('city-grid', serialize ($par));

        $cookie->expire = time()+60*60*24*180;

        Yii::app()->request->cookies['city-grid'] = $cookie;

        if(!$_GET) {

            $cookie = Yii::app()->request->cookies['city-grid'];

            $cookie = unserialize($cookie->value);





        return $dp;



Maybe this could be of help?

it was helping a bit. But in ran into troubles when clicking the ‘1’ link of the pager. The action behind this link is not sending the Daytrip_page get parameter.

For now the user can not navigate to page 1… because the script in the controller will not react and will not set the page back to 0…

any ideas?


        if (isset($_GET['Daytrip_page'])) {

            Yii::app()->user->setState('page', (int) $_GET['Daytrip_page'] - 1);




            'pagination' => array(

                'currentPage'=>Yii::app()->user->getState('page', 0)


any ideas?

How about storing the state of the CGridView in a URL such as

This may simplify things. The example URL above was generated by CGridView to be used for Ajax requests to update the page. In the example URL above, the model is Status and the columns are parameterName, deviceName, deviceIpAddress, and parameterValue.