Ibuildings Blogs![]() |
Lorna Jane Mitchell Software Engineer Lorna is a software engineer at our London office, although she mostly telecommutes from her home in Leeds. In addition to application development, she designs and delivers training for some of our UK customers, and is an expert on source control. Somehow, she also finds the time to organise the PHPNW Conference and to represent phpwomen.org in Europe. |
|
Monday, December 1. 2008Comments
Display comments as
(Linear | Threaded)
Hi Lorna,
By calling the fetchAll() method, you will fetch the whole resultset into an array, which will give you a significant performance decrease with a large resulset. The DbSelect adapter for Zend_Paginator has a nice feature where it will automaticly add a limit clause to your query, in such way that it would only query the records that you actually need. Here's an example: CODE: // ... $select = $db->from('users')->order('name'); $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select)); $paginator->setCurrentPageNumber($this->_getParam('page', 1)); $paginator->setItemCountPerPage(25); $this->view->paginator = $paginator; I hope this makes sense
I also writed a Doctrine adapter that make Zend_Paginator operate on a Doctrine_Collection object; the zf component is really versatile and the included adapter cover arrays, zend_db_select and instances of Iterator.
http://ossigeno.svn.sourceforge.net/viewvc/ossigeno/trunk/core/library/Otk/Paginator/Adapter/
Any suggestion is appreciated.
Because as a Unix guy I beware of tools that does too much, so I integrated Doctrine in my project for Domain Modelling, and not for other purposes; and because my views already use a Zend_Paginator instance.
Also Doctrine has giant static clings and I tend to wrap it in helpers or classes the more I can.
PEAR has had a paging class for years - Pager. Imaginitive name eh...?
BTW, you can use Zend_Paginator regardless of whether you're working on a "Zend Framework" project (which really is really not a black/white thing in the world of ZF anyways). Use-at-will architecture can come in handy sometimes.
Great article. ,Wil
Lorna, as Andries already mentioned, fetching the whole resultset is not efficient at all.
Here's an article I wrote a couple of years ago on the topic (using PEAR::Pager, yes, I'm biased http://www.alberton.info/pear_pager_tutorial_database_results.html I even found a little test I ran in 2004 comparing the two approaches ("fetch the full resultset and then paginate" Vs. "limit the resultset up front"). Since an image is worth a thousand words, have a look at the db load: the peaks in the image refer to a limit_query => full_query => limit_query => full_query sequence. ![]() The spike due to having to limit the query is a bit higher, because the DBMS has to filter the records, but the overall load is usually a lot lighter and the resources are freed sooner. The performance hit becomes really an issue if you use buffered queries and/or the resultset is transferred over a network. HTH
I use the pagination class from this site:
PHP pagination class Very flexible as it does just that, return an array of values to handle the rows display. Blends well with current frameworks or coding styles. Not to mention that by the looks of it's very fast. But if you are already on a commercial framework you should stick with the classes it offers.
Hi Lorna,
I hope you help me solve this problem with Zend Paginator. My problem is that I want to use paginator in 2 times, first I let it at the top of the webpage, the second is at the bottom of the webpage. But it caused an error and if I use it one time, and let it at the top or the bottom, everything works. So I don't know how to fix it. Please help me. Thank you very much |
In case anyone thought it was a bit quiet around here - that's because I've been working on a few other bits and pieces! Today I posted an article about Zend_Paginator to the Ibuildings blog. Do stop by and have a read - and read the comments as well as
Tracked: Dec 01, 18:41