ZF-10260: Zend_Paginator adapter's count()-method is called twice when using the paginationcontrol view helper

Description

When using the paginationcontrol view helper, the count()-method on the adapter get's called twice through the methods _calculatePageCount() and getTotalItemCount().

protected function _calculatePageCount()
{
    return (integer) ceil($this->getAdapter()->count() / $this->getItemCountPerPage());
}

and

public function getTotalItemCount()
{
    return count($this->getAdapter());
}

The return value of the adapter's count()-method should be held in a class member:

public function getTotalItemCount()
{
if($this->_totalItemCount === null)
        {
            $this->_totalItemCount = count($this->getAdapter());
        }
        return $this->_totalItemCount;
    }

Comments

I agree with that.

The affected functions look currently like this:


public function getTotalItemCount()
{
    return count($this->getAdapter());
}

protected function _calculatePageCount()
{
    return (integer) ceil($this->getAdapter()->count() / $this->getItemCountPerPage());
}

These could be changed as follows:


protected $_totalItemCount;

public function getTotalItemCount()
{
    if($this->_totalItemCount === null)
    {
        $this->_totalItemCount = count($this->getAdapter());
    }
    return $this->_totalItemCount;
}

protected function _calculatePageCount()
{
    $this->_totalItemCount = (integer) ceil($this->getAdapter()->count() / $this->getItemCountPerPage());
    return $this->_totalItemCount;
}

This works fine for me.