Doctrine & Zend_Paginate
01Jun09
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:
<?php
/**
* Custom Adapter for Doctrine to use with Zend_Paginator *
* @author Josh Team - http://joshteam.wordpress.com
* @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) { $this->_query->offset($offset); $this->_query->limit($itemCountPerPage); return $this->_query->execute(); } /** * * @see Countable::count() */ public function count () { $query = clone($this->_query); $query->select('count(*) as total'); $query->limit(0); $query->offset(0); $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) ); $paginator->setItemCountPerPage(($itemsPerPage) ? ($itemsPerPage) : self::PAGINATOR_ITEM_PER_PAGE ); $paginator->setCache(SpottedHere_Cache::factory()); return $paginator; }
Filed under: Development, Framework, PHP, Uncategorized, Zend Framework | 5 Comments
Tags: doctrine, PHP, Zend Framework, zend paginate, zend_paginate, ZF
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’);
$query->limit(0);
$query->offset(0);
$sql = $query->getSql();
$conn = $query->getConnection();
$r = $conn->execute( $sql )->fetch();
return $r[0];
}
Thanks, Josh! This is just what I needed and it works great!
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