views:

56

answers:

1

Using Zend Paginator and the paginator cache works fine, but the same cached pages are returned for everything. Ie. I first look at a list of articles, when i go to view the categories the articles list is returned. How can I tell the paginator which result set i am looking for?

Also, how can I clear the paginated results without re-querying the paginator. Ie. I am updated a news article therefore the pagination needs to be cleared.

Thanks

+2  A: 

Zend_Paginator uses two methods to define cache ID: _getCacheId and _getCacheInternalId. Second function is calculating cache ID based on two parameters: the number of items per page and special hash of the adapter object. The first function (_getCacheId) is calculating cache ID using result from _getCacheInternalId and current page.

So, if you are using two different paginator objects with 3 same internal parameters: adapter, current page number and the number of items per page, then your cache ID will be the same for these two objects.

So the only way I see is to define you own paginator class inherited from Zend_Paginator and to re-define one of these two internal functions to add a salt to cache ID. Something like this:

class My_Paginator extends Zend_Paginator {

    protected $_cacheSalt = '';

    public static function factory($data, $adapter = self::INTERNAL_ADAPTER, array $prefixPaths = null) {
    $paginator = parent::factory($data, $adapter, $prefixPaths);
    return new self($paginator->getAdapter());
}

    public function setCacheSalt($salt) {
        $this->_cacheSalt = $salt;
        return $this;
    }

    public function getCacheSalt() {
        return $this->_cacheSalt;
    }

    protected function _getCacheId($page = null) {
        $cacheSalt = $this->getCacheSalt();
        if ($cacheSalt != '') {
            $cacheSalt = '_' . $cacheSalt;
        }
        return parent::_getCacheId($page) . $cacheSalt;
    }
}

$articlesPaginator = My_Paginator::factory($articlesSelect, 'DbSelect');
$articlesPaginator->setCacheSalt('articles');

$categoriesSelect = My_Paginator::factory($categoriesSelect, 'DbSelect');
$articlesPaginator->setCacheSalt('categories');
Ololo
This doesn't seem to work for me, partly because it is running a function which doesn't exist in the parent (static class problem?)..Zend_Paginator::setCacheSalt() doesn't exist. No matter, I can get around it using a public variable. But the _getCacheId doesn't get called? Surely Zend thought about this - why wouldn't I use the same fetch method and items per page count?
Ashley