+ Reply to Thread
Results 1 to 7 of 7

Thread: Using Zend_Pagination with Zend_Db_Table_Abstract

  1. #1
    Sypher is offline Junior Member
    Join Date
    Jan 2009
    Posts
    4

    Question [Solved] Using Zend_Pagination with Zend_Db_Table_Abstract

    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.

  2. #2
    Eugen is offline Senior Member
    Join Date
    Sep 2008
    Location
    Croatia
    Posts
    400

    Default

    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)..

  3. #3
    armandop is offline Member
    Join Date
    Nov 2008
    Location
    San Jose
    Posts
    35

    Default

    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

  4. #4
    Sypher is offline Junior Member
    Join Date
    Jan 2009
    Posts
    4

    Default

    Thanks for your replies

    Quote Originally Posted by Eugen View Post
    1st: u dont have any code about paginator here..
    Correct, I removed it, the real question was: what do I have to add

    Quote Originally Posted by Eugen View Post
    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)..
    Hmm yeah that isn't really what I hoped for, as some tables will grow up to > 1.000.000 records in size

    Quote Originally Posted by armandop View Post
    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.
    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()".

  5. #5
    Sypher is offline Junior Member
    Join Date
    Jan 2009
    Posts
    4

    Default

    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:
    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] =>
    I cannot access these vars because they are protected. Also, shouldn't the _pageItems be filled with ... for instance the items?

    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?

  6. #6
    Eugen is offline Senior Member
    Join Date
    Sep 2008
    Location
    Croatia
    Posts
    400

    Default

    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]

  7. #7
    Sypher is offline Junior Member
    Join Date
    Jan 2009
    Posts
    4

    Default

    I solved it, thanks for the replies.

+ Reply to Thread

Similar Threads

  1. Join with Zend_Db_Table_Abstract ??
    By Znos in forum Databases
    Replies: 0
    Last Post: 02-16-2010, 07:33 PM
  2. Models and Zend_Db_Table_Abstract
    By dr.casper.black in forum Concepts, Ideas, Planning
    Replies: 0
    Last Post: 12-03-2009, 03:24 PM
  3. Zend_Db_Table_Abstract example please
    By jake142 in forum Databases
    Replies: 0
    Last Post: 09-30-2009, 08:47 PM
  4. Question about the use of Zend_Db_Table_abstract
    By jsanders in forum Databases
    Replies: 1
    Last Post: 04-24-2008, 07:39 PM
  5. Zend_XmlRpc_Server and Zend_Db_Table_Abstract
    By ingmar.heinrich in forum Web & Web Services
    Replies: 0
    Last Post: 02-22-2008, 08:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts