[Solar-talk] pager + controller + model + view
Rodrigo Moraes
rodrigo.moraes at gmail.com
Mon Jul 2 12:50:34 CDT 2007
Hey,
Let me share a solution to make controller, model and view walk
together regarding pagination. It's also a suggestion for
Solar_App_Base, or just take it as an idea to integrate well how
controllers request data from models and at the same time tell the
view about paging, page, etc.
You may think I'm obsessed by pagination :) but about half or more of
my pages have some kind of pagination; thus I've spent some time to
make it easy to handle and came up with some tricks. I hope they are
worth; here are them:
App_Base pager property:
* $layout_pager: array with pager values ('page', 'paging', 'count')
App_Base pager method:
* _setPager(): checks the 'page' and 'per_page' requests using
localizable query strings, and sets values for $layout_pager['page']
(defaults to 1) and $layout_pager['paging']. In my implementation [1],
it has 4 optional parameters which:
1. allow/disallow paging to be set by request;
2. sets a default when paging request is turned off or there's no
paging request.
3. restrict requested paging to a maximum if paging request is allowed.
4. restricts paging request to a multiple of N if paging request is allowed.
How to integrate it with models and views? This is the nice part.
Before fetching from a model, you 'setup' the pager info and then
models and views have everything they need. Here's an example:
// Set the pager info using the defaults.
$this->_setPager();
// Set page and paging.
$params = array();
$params['page'] = $this->layout_pager['page'];
$params['paging'] = $this->layout_pager['paging'];
// more params...
// Fetch the item list.
$this->items = $model->fetchAll($params);
or, why not:
// Set the pager info using the defaults.
$this->_setPager();
// Set page and paging.
$params = $this->layout_pager;
// more params...
// Fetch the item list.
$this->items = $model->fetchAll($params);
Since the controller variable has the same keys used in model
parameters ('paging', 'page'), you can just start the model $params
using $this->layout_pager, like in this second example.
As you see, in two or three lines you can set pager variables in the
controller and have them available to be used by models and views. Ah,
btw, it's super easy to output a pagination bar in your view:
echo $this->pager()->fetch($this->layout_pager);
(current pager helper doesn't allow an array as parameter. I use it in
a helper that wraps it; if Solar_App_Base had a $layout_pager var, I
would have used an array instead; it makes really easy calling the
pager - you can forget about pager helper parameters)
Besides that, if you turn on paging set by request and add an input
field to let users set/choose a 'per page' value, the requested value
will be used, filtered by the maximum allowed value and using
'multiples' if you decide to use it.
All of this is set in two or three lines when fetching some data, and
you can forget about paging forever (I hope, haha). :)
Please let me know what you think! :)
-- rodrigo
The_setPager() method is defined in this class:
[1] http://dev.tipos.org/browser/trunk/Tipos/App/Base.php
More information about the Solar-talk
mailing list