Sunday 23 September 2007, 00:07
Zend Framework Pagination, third strike
By Geoffrey - Coding - Permalink
UPDATE: new version (r105) available.
The component was given a little rewrite as expected, but maybe a little bit later than I would have wanted to :-) So it now has its own Rowclass proxy from which you can pull various infos such as current page, page range, next page, etc all exposed as getter methods (that is, getCurrentPage, getPageRange, getNextPage, etc), which you won't really have to worry about since the brand new view helper will take care of that for you. Usage has changed a little, bit, so let's first have a look at what's happening from the controller point of view:
$table = new Riskle_Db_Table_Paginate(new Table, $this->_getParam('page'));
$this->view->rowset = $table->fetchAll();
Not much changed here, except we don't need anymore to call getPaginationInfos(). Nice ! Now the big part, the view:
$this->paginate($this->rowset);
echo $this->paginate()->previous();
echo $this->paginate()->navigation();
echo $this->paginate()->next();
The view helper uses the neat composite helper trick from naneau - which is a really cool trick, great job naneau my fellow no-more-a-bunny. The first call inits the helper, feeding him the necessary rowset to work on, then you just have to call the methods you need to draw the navigation links. As you may expect, previous and next method will return nothing if no page is available (actually, they return their second argument, which defaults to an empty string).
Also, it's worth noting that the bundled Riskle_Db_Table features the patch from Erik, as well as a totally rewritten fetchCols method (now uses a straight Zend_Db_Select object instead of the ugly trick it used to use).
My code is no longer available on subversion, I moved the project to assembla. Instead ou can download this component from the riskle space's files board (direct download), the file contains all classes needed for the component to work, just unzip it in your include_path and you're set.
As usual, any comments are more than welcome.
7 comments
Once again thanks for the code. I think you mean previous() instead of prev() though.
Another small fix, if you have 2 tables that don’t completely map together (ex: you have a log table that maps it’s user id to a user table, if one of the user ids doesn’t exist the log row will get ignored) the fetchAll will return the filtered rows and the fetchCount will return them all (in my case fetchAll returned 260 and fetchCount returned 262, so I had 1 extra page that had no entries)
To fix it you just need to apply the parent mapping to fetchCount, I had to also add a variable to _applyParentMap so it didn’t try to select the mapped fields when selecting the count.
http://code.ryvondesigns.com/riskle...
http://code.ryvondesigns.com/riskle...
Also, while I was attempting to fix this I might’ve stumbled upon an easier way to apply the parent map to fetchRow and fetchAll. First I removed fetchAll , fetchRow, and _makeRowset. Then I added a copy of Zend_Db_Table_Abstract’s _fetch method with _applyParentMap inside it. It seemed to work fine in my tests, I didn’t include it in the patch since I haven’t tested it much but you might want to look at it sometime.
tu composes tes articles àune heure ben tardive ! :)
Once again thanks for the patch :-) While I not found this a very clean way of doing things, it'll do the job for the moment as I can't think (did not take the time to think actually) of another way to fix the problem.
Concerning the _fetch thing, I can't believe I did not think of it earlier. Patching at the lower level seems the way to go.
As far as I know the problem stems from a MySQL limitation, not supporting FULL OUTER JOIN. I've seen some hacks that use UNIONs to get the desired result but I'm not sure how well it would translate to the framework and unfortunately don't have the time to investigate right now. If I get some free time and find a better method I'll post it. :-)
i am a newbie. where do i place the library files?
Great job on the pager. One thing that i am not clear on however, is there a way to set the _itemsPerPage property in Riskle_Db_Table_Paginate class?
I noticed also in the view helper that there was a syntax error in the default html. you start with a <p> but end with a </li> for the previous and next functions.
Hi brandon, there's no way atm to set the items per page property, I'm aware of this and this will be fixed in the next release, as well as the html syntax quirk you're pointing.
thanks for your feedback
This post's comments feed