Doctrine & Zend_Paginate


So, I personally am a huge fan of the doctrine project. And, seeing as I am using Zend Framework I utilize Zend_Paginate in my service layer. All that being said, let me introduce you to my Doctrine Paginator Adapter:

 * Custom Adapter for Doctrine to use with Zend_Paginator
 * @author Josh Team -
 * @version 1
class SpottedHere_Paginator_Adapter_Doctrine 
                 implements Zend_Paginator_Adapter_Interface

    protected $_query;

    public function __construct(
                 Doctrine_Query_Abstract $doctrineQuery)
        $this->_query = $doctrineQuery;

     * @param integer $offset Page offset
     * @param integer $itemCountPerPage Number of items per page
     * @return array
     * @see Zend_Paginator_Adapter_Interface::getItems()
    public function getItems ($offset, $itemCountPerPage)
        return $this->_query->execute();
     * @see Countable::count()
    public function count ()
        $query = clone($this->_query);
        $query->select('count(*) as total');
        $rs = $query->fetchOne(array(), Doctrine::HYDRATE_ARRAY);
        return $rs['total'];
/* ---- THEN TO USE ---- */
    protected function queryToPaginator( 
           Doctrine_Query_Abstract $query, int $itemsPerPage = null)

    {         $paginator = new Zend_Paginator( 

           new SpottedHere_Paginator_Adapter_Doctrine($query) );
           ($itemsPerPage) ? ($itemsPerPage) : self::PAGINATOR_ITEM_PER_PAGE );
        return $paginator;

5 Responses to “Doctrine & Zend_Paginate”

  1. Great job!
    Yesterday I started with zf and doctrine and the first experience was with the zend_paginator. I’ve a cuestion, when i try to read the records from de view (phtml) with this code:

    paginator as $item): ?>

    I get the records stored in the paginator as array, not as objects.

    thanks.. desde ya, muchas gracias 😉

    • It’s more than likely the query object you are creating with Doctrine, make sure you aren’t hydrating it with an array.

      • Take a look,

        there is a problem with the clone() method and the Doctrine_Query objetct related with a HYDRATE_ARRAY option.

        When you execute the query clone at first time with HYDRATE_ARRAY option in the count() method, affects the HYDRATE_ARRAY option of the second execution in the getItems() method (the original query object was affected).

        I don’t know why it happens but i “hack” the yout code to solve this problem, in the count method:

        public function count ()
        $query = clone $this->_query;
        $query->select(‘count(*) as total’);

        $sql = $query->getSql();

        $conn = $query->getConnection();

        $r = $conn->execute( $sql )->fetch();

        return $r[0];

  2. 4 Joe Bob

    Thanks, Josh! This is just what I needed and it works great!

  3. 5 Kamlesh Bhure

    I trying to use above method which is very nice of pagination but I want to make it work without using Doctrine Library. is it possible to use zend’s native library of Database. If yes can you post example.
    I am using zend version 1.11.2

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: