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,
'pagination'=>array(
//'currentPage'=>$page,
'pageSize'=>Yii::app()->params['itemsPerPage'],
),
'sort' => array(
'defaultOrder' => 'id_city',
'attributes' => array(
'*',
'country' => array(
'asc' => 'country.country',
'desc' => 'country.country 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);
$dp->setPagination($cookie['pagination']);
$dp->setCriteria($cookie['criteria']);
$dp->setSort($cookie['sort']);
}
return $dp;
}