1st: u dont have any code about paginator here..
2nd: it should return all rows since zend_paginator is working with data u provide to him (it doesnt fetch 10 by 10 rows, it loads all in and then he splits it in pages)..
Hi,
I hope this is posted in the right category. If not, please move it to a more appropriate one.
I'm currently working on a web application which will involve quite a few database entries. In order to make it easier to work, and less stressful for the system, I wanted to integrate Zend_Pagination.
I Google'd, read the Zend Wiki and manual but I can't get it to work.
My structure is:
Controller:
[php]
function indexAction() {
$this->view->data= $this->datas->fetchuserdata( $this->userid );
}
[/php]
Nothing much really.
the $this->data is "new datas();" which has the following code:
[php]
<?
class datas extends Zend_Db_Table_Abstract
{
protected $_name = 'data';
protected $_primary = 'data_id';
protected $_rowClass = 'data';
public function fetchuserdata($id)
{
$id = (int)$id;
return $this->fetchAll('user_id = '. $id);
}
}
[/php]
Currently this is working perfectly, but it returns all the rows. During development there aren't that many, but after a while this will be quite a big table.
I tried various things, but I am unable to get proper pagination. At one point I did get pagination by adding the Paginator Factory to the fetchuserdata function in my model.
It did return the maximum values I set in the Paginator, and ?page=2 did return the ones on page 2. Problem was: I couldn't get the paginator helper to show the current pages.
How can I add pagination properly to my system? I hope someone can help me out and give me a clue on how to do this.
Thanks!
Last edited by Sypher; 01-29-2009 at 02:57 PM. Reason: Solved.
1st: u dont have any code about paginator here..
2nd: it should return all rows since zend_paginator is working with data u provide to him (it doesnt fetch 10 by 10 rows, it loads all in and then he splits it in pages)..
Sypher - you need to use a Zend_Db_Select object to accomplish what you want. fetchAll will return an Array. This will return ALL of the records which can be inefficient. Using the Zend_Db_Select object will return only a few at a time. I wrote up a small example. { Online Notes } Blog Archive Zend_Paginator and Your Result Set.
www.armando.ws
Author: Beginning Zend Framework
Thanks for your replies
Correct, I removed it, the real question was: what do I have to add
Hmm yeah that isn't really what I hoped for, as some tables will grow up to > 1.000.000 records in size
Yeah I came across your page when I Googled
I already tried a Zend_Db_Select object, and it did work. But I couldn't get pagination to work properly.
The pagination code should be added to my "fetchuserdata($id)" function right? This should become a "fetchuserdata($id, $currentPage)", so it knows on which page it is.
It indeed returns only an X amount of entries, but I couldn't get the pagination to work: Page 1 | 2 | 3 stuff.
I tried to adapt the examples in the Zend Manual, but it complained about things like: "hasPages()".
So I came up with this:
Model: Datas.php
[php]
<?
class Datas extends Zend_Db_Table_Abstract
{
protected $_name = 'data';
protected $_primary = 'data_id';
protected $_rowClass = 'Data';
public function fetchuserdata_Page($id, $currentPage)
{
$selectStatement = $this->select('user_id = '. $id)
->from($this->_name);
$Paginator = Zend_Paginator::factory( new Zend_Paginator_Adapter_DbSelect( $selectStatement ) );
$Paginator->setCurrentPageNumber($currentPage);
$Paginator->setItemCountPerPage(3);
$Paginator->setPageRange(5);
return $Paginator;
}
}
[/php]
The Controller (specific the action):
[php]
function indexAction() {
$pageNumber = (int)$this->_request->getParam('page', 1);
$data = $this->datas->fetchuserdata_Page( $this->userid, $pageNumber );
$this->view->data= $data;
}
[/php]
This does return only 3 items. But when I do a print_r on $data it gives back some odd data at the end:
I cannot access these vars because they are protected. Also, shouldn't the _pageItems be filled with ... for instance the items?Code:[_rowCount:protected] => 5 ) [_currentItemCount:protected] => [_currentItems:protected] => [_currentPageNumber:protected] => 1 [_itemCountPerPage:protected] => 3 [_pageCount:protected] => 2 [_pageItems:protected] => Array ( ) [_pageRange:protected] => 10 [_pages:protected] => [_view:protected] =>
It *is* a Zend_Paginator Object, instead of a Zend_Db_Table_Rowset Object which I had before.
Now what? I have the feeling I'm quite close in achieving what I am trying to do, but I don't really know how to proceed.
Do you guys have any ideas for me?![]()
what you are using is db_table_select, not db_select (there is a diference, and there are 2 adapters, one for zend_db_table_select and one for zend_db_select)..
you need to get db adapter (i hope u have it in your registry)
[PHP]$db = Zend_Registry::get('db');
$select = $db->select()->from('data');
$paginator = Zend_Paginator::factory($select);[/PHP]
I solved it, thanks for the replies.