2
0

Zend_Paginator-Advanced.xml 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.paginator.advanced">
  4. <title>Advanced usage</title>
  5. <sect2 id="zend.paginator.advanced.adapters">
  6. <title>Custom data source adapters</title>
  7. <para>
  8. At some point you may run across a data type that is not covered by
  9. the packaged adapters. In this case, you will need to write your
  10. own.
  11. </para>
  12. <para>
  13. To do so, you must implement
  14. <classname>Zend_Paginator_Adapter_Interface</classname>. There are two
  15. methods required to do this:
  16. </para>
  17. <itemizedlist>
  18. <listitem>
  19. <para>count()</para>
  20. </listitem>
  21. <listitem>
  22. <para>getItems($offset, $itemCountPerPage)</para>
  23. </listitem>
  24. </itemizedlist>
  25. <para>
  26. Additionally, you'll want to implement a constructor that takes
  27. your data source as a parameter and stores it as a protected or
  28. private property. How you wish to go about doing this
  29. specifically is up to you.
  30. </para>
  31. <para>
  32. If you've ever used the SPL interface
  33. <ulink url="http://www.php.net/~helly/php/ext/spl/interfaceCountable.html"><code>Countable</code></ulink>,
  34. you're familiar with <code>count()</code>. As used with
  35. <classname>Zend_Paginator</classname>, this is the total number of items
  36. in the data collection.
  37. Additionally, the <classname>Zend_Paginator</classname> instance provides a method
  38. <code>countAllItems()</code> that proxies to the adapter
  39. <code>count()</code> method.
  40. </para>
  41. <para>
  42. The <code>getItems()</code> method is only slightly more
  43. complicated. For this, your adapter is supplied with an offset and
  44. the number of items to display per page. You must return the
  45. appropriate slice of data. For an array, that would be:
  46. </para>
  47. <para>
  48. <programlisting language="php"><![CDATA[
  49. return array_slice($this->_array, $offset, $itemCountPerPage);
  50. ]]></programlisting>
  51. </para>
  52. <para>
  53. Take a look at the packaged adapters (all of which implement the
  54. <classname>Zend_Paginator_Adapter_Interface</classname>) for ideas of how you
  55. might go about implementing your own.
  56. </para>
  57. </sect2>
  58. <sect2 id="zend.paginator.advanced.scrolling-styles">
  59. <title>Custom scrolling styles</title>
  60. <para>
  61. Creating your own scrolling style requires that you implement
  62. <classname>Zend_Paginator_ScrollingStyle_Interface</classname>, which defines
  63. a single method, <code>getPages()</code>. Specifically,
  64. </para>
  65. <para>
  66. <programlisting language="php"><![CDATA[
  67. public function getPages(Zend_Paginator $paginator, $pageRange = null);
  68. ]]></programlisting>
  69. </para>
  70. <para>
  71. This method should calculate a lower and upper bound for page
  72. numbers within the range of so-called "local" pages (that is, pages
  73. that are nearby the current page).
  74. </para>
  75. <para>
  76. Unless it extends another scrolling style (see
  77. <classname>Zend_Paginator_ScrollingStyle_Elastic</classname> for an example),
  78. your custom scrolling style will inevitably end with something
  79. similar to the following line of code:
  80. </para>
  81. <para>
  82. <programlisting language="php"><![CDATA[
  83. return $paginator->getPagesInRange($lowerBound, $upperBound);
  84. ]]></programlisting>
  85. </para>
  86. <para>
  87. There's nothing special about this call; it's merely a convenience
  88. method to check the validity of the lower and upper bound and
  89. return an array of the range to the paginator.
  90. </para>
  91. <para>
  92. When you're ready to use your new scrolling style, you'll need to
  93. tell <classname>Zend_Paginator</classname> what directory to look in. To do
  94. that, do the following:
  95. </para>
  96. <para>
  97. <programlisting language="php"><![CDATA[
  98. $prefix = 'My_Paginator_ScrollingStyle';
  99. $path = 'My/Paginator/ScrollingStyle/';
  100. Zend_Paginator::addScrollingStylePrefixPath($prefix, $path);
  101. ]]></programlisting>
  102. </para>
  103. </sect2>
  104. <sect2 id="zend.paginator.advanced.caching">
  105. <title>Caching features</title>
  106. <para>
  107. <classname>Zend_Paginator</classname> can be told to cache the data it has already
  108. passed on, preventing the adapter from fetching them each time they are used.
  109. To tell paginator to automatically cache the adapter's data, just pass to
  110. its <code>setCache()</code> method a <classname>Zend_Cache_Core</classname> instance.
  111. </para>
  112. <para>
  113. <programlisting language="php"><![CDATA[
  114. $paginator = Zend_Paginator::factory($someData);
  115. $fO = array('lifetime' => 3600, 'automatic_serialization' => true);
  116. $bO = array('cache_dir'=>'/tmp');
  117. $cache = Zend_cache::factory('Core', 'File', $fO, $bO);
  118. Zend_Paginator::setCache($cache);
  119. ]]></programlisting>
  120. </para>
  121. <para>
  122. As far as <classname>Zend_Paginator</classname> has got a <classname>Zend_Cache_Core</classname> instance, data will
  123. be cached. Sometimes you would like not to cache data even if you already passed
  124. a cache instance. You should then use <code>setCacheEnable()</code> for that.
  125. </para>
  126. <para>
  127. <programlisting language="php"><![CDATA[
  128. $paginator = Zend_Paginator::factory($someData);
  129. // $cache is a Zend_Cache_Core instance
  130. Zend_Paginator::setCache($cache);
  131. // ... later on the script
  132. $paginator->setCacheEnable(false);
  133. // cache is now disabled
  134. ]]></programlisting>
  135. </para>
  136. <para>
  137. When a cache is set, data are automatically stored in it and pulled out from
  138. it. It then can be useful to empty the cache manually. You can get this done by
  139. calling <code>clearPageItemCache($pageNumber)</code>.
  140. If you don't pass any parameter, the whole cache will be empty. You can optionally
  141. pass a parameter representing the page number to empty in the cache:
  142. </para>
  143. <para>
  144. <programlisting language="php"><![CDATA[
  145. $paginator = Zend_Paginator::factory($someData);
  146. Zend_Paginator::setCache($cache);
  147. $items = $paginator->getCurrentItems();
  148. // page 1 is now in cache
  149. $page3Items = $paginator->getItemsByPage(3);
  150. // page 3 is now in cache
  151. // clear the cache of the results for page 3
  152. $paginator->clearPageItemCache(3);
  153. // clear all the cache data
  154. $paginator->clearPageItemCache();
  155. ]]></programlisting>
  156. </para>
  157. <para>
  158. Changing the item count per page will empty the whole cache
  159. as it would have become invalid:
  160. </para>
  161. <para>
  162. <programlisting language="php"><![CDATA[
  163. $paginator = Zend_Paginator::factory($someData);
  164. Zend_Paginator::setCache($cache);
  165. // fetch some items
  166. $items = $paginator->getCurrentItems();
  167. // all the cache data will be flushed:
  168. $paginator->setItemCountPerPage(2);
  169. ]]></programlisting>
  170. </para>
  171. <para>
  172. It is also possible to see the data in cache and ask for them directly.
  173. <code>getPageItemCache()</code> can be used for that:
  174. </para>
  175. <para>
  176. <programlisting language="php"><![CDATA[
  177. $paginator = Zend_Paginator::factory($someData);
  178. $paginator->setItemCountPerPage(3);
  179. Zend_Paginator::setCache($cache);
  180. // fetch some items
  181. $items = $paginator->getCurrentItems();
  182. $otherItems = $paginator->getItemsPerPage(4);
  183. // see the cached items as a two-dimension array:
  184. var_dump($paginator->getPageItemCache());
  185. ]]></programlisting>
  186. </para>
  187. </sect2>
  188. </sect1>
  189. <!--
  190. vim:se ts=4 sw=4 et:
  191. -->