Pentium10, on 17 July 2011 - 02:31 PM, said:
Cool, thanks for the suggestion.
Here's my solution, which does not use the CPagination methods as mentioned. It's a bit long, but works. (Part of the reason for extending the framework classes is I've removed another part of my solution which includes a drop-down control in each admin view to select the pageSize for this model and remember it in persistent storage, which is outside the scope of this thread.)
This solution would be trivial (just part [4]) except for the fact that CPagination omits the pageNo on the first page. This causes problems. In order to fix this you must extend CPagination, extend CActiveDataProvider to use it, and extend all your models to use the new ActiveDataProvider class.
1. extend CPagination as MyPagination and override createPageUrl to *always* include the pageNo in the URL.
class MyPagination extends CPagination
{
// always put the page number in. See MyController.actionAdmin() for usage
public function createPageUrl($controller,$page)
{
$params=$this->params===null ? $_GET : $this->params;
// if($page>0) // page 0 is the default
$params[$this->pageVar]=$page+1;
// else
// unset($params[$this->pageVar]);
return $controller->createUrl($this->route,$params);
}
}
2. extend CActiveDataProvider as MyActiveDataProvider. override getPagination to use MyPagination
class MyActiveDataProvider extends CActiveDataProvider {
private $_pagination;
// override to create instance of MyPagination
public function getPagination() {
if ($this->_pagination === null) {
//$this->_pagination=new CPagination;
$this->_pagination = new MyPagination;
if (($id = $this->getId()) != '')
$this->_pagination->pageVar = $id . '_page';
}
return $this->_pagination;
}
}
3. in all your models, change search() to use MyActiveDataProvider instead of CActiveDataProvider
public function search() {
$criteria = new CDbCriteria;
....
return new MyActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
4. (either) extend CController as MyController. Make all your controllers extend from MyController. Each controller must define $modelClass. In this case, the child controllers don't have an actionAdmin. (or) simply put the code fragment into the actionAdmin() of each controller.
public $modelClass = ''; // child must define actual Model name
...
actionAdmin()
...
// persist page number
$pageParam = ucfirst($this->modelClass) . '_page';
if (isset($_GET[$pageParam])) {
$page = $_GET[$pageParam];
Yii::app()->user->setState($this->id . '-page', (int) $page);
} else {
$page = Yii::app()->user->getState($this->id . '-page', 1);
$_GET[$pageParam] = $page;
}
...