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

Custom data source adapters

At some point you may run across a data type that is not covered by the packaged adapters. In this case, you will need to write your own.

To do so, you must implement Zend_Paginator_Adapter_Interface. There are two methods required to do this:

  • count()

  • getItems($offset, $itemCountPerPage)

Additionally, you'll want to implement a constructor that takes your data source as a parameter and stores it as a protected or private property. How you wish to go about doing this specifically is up to you.

If you've ever used the SPL interface Countable, you're familiar with count(). As used with Zend_Paginator, this is the total number of items in the data collection. Additionally, the Zend_Paginator instance provides a method countAllItems() that proxies to the adapter count() method.

The getItems() method is only slightly more complicated. For this, your adapter is supplied with an offset and the number of items to display per page. You must return the appropriate slice of data. For an array, that would be:

return array_slice($this->_array$offset$itemCountPerPage);

Take a look at the packaged adapters (all of which implement the Zend_Paginator_Adapter_Interface) for ideas of how you might go about implementing your own.

Zend Framework