Zend_Pagination adapter for Propel

Like recently proposed by Jason Eisenmenger, I’ve written a Zend_Pagination adapter for Propel.

It uses the DbFinder symfony plugin (formerly sfPropelFinder) which mimics Doctrine’s way of performing queries. The strictlyPHP_Propel_Finder used below is just a wrapper around that plugin.

Please note: it can certainly be improved (and I’m not sure the Zend Framework coding standards are met).

class strictlyPHP_Paginator_Adapter_Propel implements
Zend_Paginator_Adapter_Interface
{
    /**
     * @var strictlyPHP_Propel_Finder
     */
    protected $_finder;

    /**
     * @var Criteria
     */
    protected $_criteria;

    /**
     * @var int
     */
    protected $_count;

    /**
     * Constructor
     * @param strictlyPHP_Propel_Finder $finder
     */
    public function __construct($finder)
    {
        $this->_finder = $finder;
        $this->_criteria = $finder->getCriteria();
    }

    /**
     * Get count
     * @return int
     */
    public function count()
    {
         if ($this->_count === null) {
             $this->_finder->setCriteria($this->_criteria);
            $this->_count = $this->_finder->count();
         }

        return $this->_count;
    }

    /**
     * Get items
     * @param int $pageNumber
     * @param int $itemCountPerPage
     * @return array
     */
    public function getItems($pageNumber, $itemCountPerPage)
    {
        $criteria = clone $this->_criteria;
        $criteria->setOffset($pageNumber);
        $criteria->setLimit($itemCountPerPage);
        return $this->_finder->setCriteria($criteria)->find();
    }
}

Tags: ,

 
 
 

Leave a Reply