PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Caching features

Zend_Paginator can be told to cache the data it has already passed on, preventing the adapter from fetching them each time they are used. To tell paginator to automatically cache the adapter's data, just pass to its setCache() method a Zend_Cache_Core instance.

<?php
$paginator 
Zend_Paginator::factory($someData);
$fO = array('lifetime' => 3600'automatic_serialization' => true);
$bO = array('cache_dir'=>'/tmp');
$cache Zend_cache::factory('Core''File'$fO$bO);
Zend_Paginator::setCache($cache);

As far as Zend_Paginator has got a Zend_Cache_Core instance, data will be cached. Sometimes you would like not to cache data even if you already passed a cache instance. You should then use setCacheEnable() for that.

<?php
$paginator 
Zend_Paginator::factory($someData);
// $cache is a Zend_Cache_Core instance
Zend_Paginator::setCache($cache);
// ... later on the script
$paginator->setCacheEnable(false);
// cache is now disabled

When a cache is set, data are automatically stored in it and pulled out from it. It then can be useful to empty the cache manually. You can get this done by calling clearPageItemCache($pageNumber). If you don't pass any parameter, the whole cache will be empty. You can optionally pass a parameter representing the page number to empty in the cache:

<?php
$paginator 
Zend_Paginator::factory($someData);
Zend_Paginator::setCache($cache);
$items $paginator->getCurrentItems();
// page 1 is now in cache
$page3Items $paginator->getItemsByPage(3);
// page 3 is now in cache

// clear the cache of the results for page 3
$paginator->clearPageItemCache(3);

// clear all the cache data
$paginator->clearPageItemCache();

Changing the item count per page will empty the whole cache as it would have become invalid:

<?php
$paginator 
Zend_Paginator::factory($someData);
Zend_Paginator::setCache($cache);
// fetch some items
$items $paginator->getCurrentItems();

// all the cache data will be flushed:
$paginator->setItemCountPerPage(2);

It is also possible to see the data in cache and ask for them directly. getPageItemCache() can be used for that:

<?php
$paginator 
Zend_Paginator::factory($someData);
$paginator->setItemCountPerPage(3);
Zend_Paginator::setCache($cache);

// fetch some items
$items $paginator->getCurrentItems();
$otherItems $paginator->getItemsPerPage(4);

// see the cached items as a two-dimension array:
var_dump($paginator->getPageItemCache());

Zend Framework