Zend_Db_Table_Rowset.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.db.table.rowset">
  4. <title>Zend_Db_Table_Rowset</title>
  5. <sect2 id="zend.db.table.rowset.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. When you run a query against a Table class using the <code>find()</code> or <code>fetchAll()</code>
  9. methods, the result is returned in an object of type <classname>Zend_Db_Table_Rowset_Abstract</classname>. A Rowset
  10. contains a collection of objects descending from <classname>Zend_Db_Table_Row_Abstract</classname>. You can iterate
  11. through the Rowset and access individual Row objects, reading or modifying data in the Rows.
  12. </para>
  13. </sect2>
  14. <sect2 id="zend.db.table.rowset.fetch">
  15. <title>Fetching a Rowset</title>
  16. <para>
  17. <classname>Zend_Db_Table_Abstract</classname> provides methods <code>find()</code> and <code>fetchAll()</code>, each
  18. of which returns an object of type <classname>Zend_Db_Table_Rowset_Abstract</classname>.
  19. </para>
  20. <example id="zend.db.table.rowset.fetch.example">
  21. <title>Example of fetching a rowset</title>
  22. <programlisting role="php"><![CDATA[
  23. $bugs = new Bugs();
  24. $rowset = $bugs->fetchAll("bug_status = 'NEW'");
  25. ]]></programlisting>
  26. </example>
  27. </sect2>
  28. <sect2 id="zend.db.table.rowset.rows">
  29. <title>Retrieving Rows from a Rowset</title>
  30. <para>
  31. The Rowset itself is usually less interesting than the Rows that it contains. This section illustrates how
  32. to get the Rows that comprise the Rowset.
  33. </para>
  34. <para>
  35. A legitimate query returns zero rows when no rows in the database match the query conditions. Therefore, a
  36. Rowset object might contain zero Row objects. Since <classname>Zend_Db_Table_Rowset_Abstract</classname> implements
  37. the <code>Countable</code> interface, you can use <code>count()</code> to determine the number of Rows in
  38. the Rowset.
  39. </para>
  40. <example id="zend.db.table.rowset.rows.counting.example">
  41. <title>Counting the Rows in a Rowset</title>
  42. <programlisting role="php"><![CDATA[
  43. $rowset = $bugs->fetchAll("bug_status = 'FIXED'");
  44. $rowCount = count($rowset);
  45. if ($rowCount > 0) {
  46. echo "found $rowCount rows";
  47. } else {
  48. echo 'no rows matched the query';
  49. }
  50. ]]></programlisting>
  51. </example>
  52. <example id="zend.db.table.rowset.rows.current.example">
  53. <title>Reading a Single Row from a Rowset</title>
  54. <para>
  55. The simplest way to access a Row from a Rowset is to use the <code>current()</code> method. This is
  56. particularly appropriate when the Rowset contains exactly one Row.
  57. </para>
  58. <programlisting role="php"><![CDATA[
  59. $bugs = new Bugs();
  60. $rowset = $bugs->fetchAll("bug_id = 1");
  61. $row = $rowset->current();
  62. ]]></programlisting>
  63. </example>
  64. <para>
  65. If the Rowset contains zero rows, <code>current()</code> returns
  66. PHP's <code>null</code> value.
  67. </para>
  68. <example id="zend.db.table.rowset.rows.iterate.example">
  69. <title>Iterating through a Rowset</title>
  70. <para>
  71. Objects descending from <classname>Zend_Db_Table_Rowset_Abstract</classname> implement the <code>SeekableIterator</code>
  72. interface, which means you can loop through them using the <code>foreach</code> construct. Each value
  73. you retrieve this way is a <classname>Zend_Db_Table_Row_Abstract</classname> object that corresponds to one
  74. record from the table.
  75. </para>
  76. <programlisting role="php"><![CDATA[
  77. $bugs = new Bugs();
  78. // fetch all records from the table
  79. $rowset = $bugs->fetchAll();
  80. foreach ($rowset as $row) {
  81. // output 'Zend_Db_Table_Row' or similar
  82. echo get_class($row) . "\n";
  83. // read a column in the row
  84. $status = $row->bug_status;
  85. // modify a column in the current row
  86. $row->assigned_to = 'mmouse';
  87. // write the change to the database
  88. $row->save();
  89. }
  90. ]]></programlisting>
  91. </example>
  92. <example id="zend.db.table.rowset.rows.seek.example">
  93. <title>Seeking to a known position into a Rowset</title>
  94. <para>
  95. <code>SeekableIterator</code> allows you to seek to a position that you would like the iterator to jump to.
  96. Simply use the <code>seek()</code> method for that. Pass it an integer representing the number of the Row
  97. you would like your Rowset to point to next, don't forget that it starts with index 0. If the index is wrong,
  98. ie doesn't exist, an exception will be thrown. You should use <code>count()</code> to check the number of
  99. results before seeking to a position.
  100. </para>
  101. <programlisting role="php"><![CDATA[
  102. $bugs = new Bugs();
  103. // fetch all records from the table
  104. $rowset = $bugs->fetchAll();
  105. // takes the iterator to the 9th element (zero is one element) :
  106. $rowset->seek(8);
  107. // retrieve it
  108. $row9 = $rowset->current();
  109. // and use it
  110. $row9->assigned_to = 'mmouse';
  111. $row9->save();
  112. ]]></programlisting>
  113. </example>
  114. <para>
  115. <code>getRow()</code> allows you to get a specific row in the Rowset, knowing its position; don't forget
  116. however that positions start with index zero. The first parameter for <code>getRow()</code> is an integer
  117. for the position asked. The second optional parameter is a boolean; it tells the Rowset iterator if it must
  118. seek to that position in the same time, or not (default is false). This method returns a <classname>Zend_Db_Table_Row</classname>
  119. object by default. If the position requested does not exist, an exception will be thrown. Here is an example :
  120. </para>
  121. <programlisting role="php"><![CDATA[
  122. $bugs = new Bugs();
  123. // fetch all records from the table
  124. $rowset = $bugs->fetchAll();
  125. // retrieve the 9th element immediately:
  126. $row9->getRow(8);
  127. // and use it:
  128. $row9->assigned_to = 'mmouse';
  129. $row9->save();
  130. ]]></programlisting>
  131. <para>
  132. After you have access to an individual Row object, you can manipulate the Row using methods described in
  133. <xref linkend="zend.db.table.row" />.
  134. </para>
  135. </sect2>
  136. <sect2 id="zend.db.table.rowset.to-array">
  137. <title>Retrieving a Rowset as an Array</title>
  138. <para>
  139. You can access all the data in the Rowset as an array using the <code>toArray()</code> method of the Rowset
  140. object. This returns an array containing one entry per Row. Each entry is an associative array having keys
  141. that correspond to column names and elements that correspond to the respective column values.
  142. </para>
  143. <example id="zend.db.table.rowset.to-array.example">
  144. <title>Using toArray()</title>
  145. <programlisting role="php"><![CDATA[
  146. $bugs = new Bugs();
  147. $rowset = $bugs->fetchAll();
  148. $rowsetArray = $rowset->toArray();
  149. $rowCount = 1;
  150. foreach ($rowsetArray as $rowArray) {
  151. echo "row #$rowCount:\n";
  152. foreach ($rowArray as $column => $value) {
  153. echo "\t$column => $value\n";
  154. }
  155. ++$rowCount;
  156. echo "\n";
  157. }
  158. ]]></programlisting>
  159. </example>
  160. <para>
  161. The array returned from <code>toArray()</code> is not updateable. That is, you can modify values in the
  162. array as you can with any array, but changes to the array data are not propagated to the database.
  163. </para>
  164. </sect2>
  165. <sect2 id="zend.db.table.rowset.serialize">
  166. <title>Serializing and Unserializing a Rowset</title>
  167. <para>
  168. Objects of type <classname>Zend_Db_Table_Rowset_Abstract</classname> are serializable. In a similar fashion to
  169. serializing an individual Row object, you can serialize a Rowset and unserialize it later.
  170. </para>
  171. <example id="zend.db.table.rowset.serialize.example.serialize">
  172. <title>Serializing a Rowset</title>
  173. <para>
  174. Simply use PHP's <code>serialize()</code> function to create a string containing a byte-stream
  175. representation of the Rowset object argument.
  176. </para>
  177. <programlisting role="php"><![CDATA[
  178. $bugs = new Bugs();
  179. $rowset = $bugs->fetchAll();
  180. // Convert object to serialized form
  181. $serializedRowset = serialize($rowset);
  182. // Now you can write $serializedRowset to a file, etc.
  183. ]]></programlisting>
  184. </example>
  185. <example id="zend.db.table.rowset.serialize.example.unserialize">
  186. <title>Unserializing a Serialized Rowset</title>
  187. <para>
  188. Use PHP's <code>unserialize()</code> function to restore a string containing a byte-stream
  189. representation of an object. The function returns the original object.
  190. </para>
  191. <para>
  192. Note that the Rowset object returned is in a <emphasis>disconnected</emphasis> state. You can iterate
  193. through the Rowset and read the Row objects and their properties, but you cannot change values in the
  194. Rows or execute other methods that require a database connection (for example, queries against related
  195. tables).
  196. </para>
  197. <programlisting role="php"><![CDATA[
  198. $rowsetDisconnected = unserialize($serializedRowset);
  199. // Now you can use object methods and properties, but read-only
  200. $row = $rowsetDisconnected->current();
  201. echo $row->bug_description;
  202. ]]></programlisting>
  203. </example>
  204. <note>
  205. <title>Why do Rowsets unserialize in a disconnected state?</title>
  206. <para>
  207. A serialized object is a string that is readable to anyone who possesses it. It could be a security
  208. risk to store parameters such as database account and password in plain, unencrypted text in the
  209. serialized string. You would not want to store such data to a text file that is not protected, or send
  210. it in an email or other medium that is easily read by potential attackers. The reader of the serialized
  211. object should not be able to use it to gain access to your database without knowing valid credentials.
  212. </para>
  213. </note>
  214. <para>
  215. You can reactivate a disconnected Rowset using the <code>setTable()</code> method. The argument to this
  216. method is a valid object of type <classname>Zend_Db_Table_Abstract</classname>, which you create. Creating a Table
  217. object requires a live connection to the database, so by reassociating the Table with the Rowset, the
  218. Rowset gains access to the database. Subsequently, you can change values in the Row objects contained in
  219. the Rowset and save the changes to the database.
  220. </para>
  221. <example id="zend.db.table.rowset.serialize.example.set-table">
  222. <title>Reactivating a Rowset as Live Data</title>
  223. <programlisting role="php"><![CDATA[
  224. $rowset = unserialize($serializedRowset);
  225. $bugs = new Bugs();
  226. // Reconnect the rowset to a table, and
  227. // thus to a live database connection
  228. $rowset->setTable($bugs);
  229. $row = $rowset->current();
  230. // Now you can make changes to the row and save them
  231. $row->bug_status = 'FIXED';
  232. $row->save();
  233. ]]></programlisting>
  234. </example>
  235. <para>
  236. Reactivating a Rowset with <code>setTable()</code> also reactivates all the Row objects contained in that
  237. Rowset.
  238. </para>
  239. </sect2>
  240. <sect2 id="zend.db.table.rowset.extending">
  241. <title>Extending the Rowset class</title>
  242. <para>
  243. You can use an alternative concrete class for instances of Rowsets
  244. by extending <classname>Zend_Db_Table_Rowset_Abstract</classname>. Specify the custom
  245. Rowset class by name either in the <code>$_rowsetClass</code>
  246. protected member of a Table class, or in the array argument of the
  247. constructor of a Table object.
  248. </para>
  249. <example id="zend.db.table.rowset.extending.example">
  250. <title>Specifying a custom Rowset class</title>
  251. <programlisting role="php"><![CDATA[
  252. class MyRowset extends Zend_Db_Table_Rowset_Abstract
  253. {
  254. // ...customizations
  255. }
  256. // Specify a custom Rowset to be used by default
  257. // in all instances of a Table class.
  258. class Products extends Zend_Db_Table_Abstract
  259. {
  260. protected $_name = 'products';
  261. protected $_rowsetClass = 'MyRowset';
  262. }
  263. // Or specify a custom Rowset to be used in one
  264. // instance of a Table class.
  265. $bugs = new Bugs(array('rowsetClass' => 'MyRowset'));
  266. ]]></programlisting>
  267. </example>
  268. <para>
  269. Typically, the standard <classname>Zend_Db_Rowset</classname> concrete class is
  270. sufficient for most usage. However, you might find it useful
  271. to add new logic to a Rowset, specific to a given Table.
  272. For example, a new method could calculate an aggregate
  273. over all the Rows in the Rowset.
  274. </para>
  275. <example id="zend.db.table.rowset.extending.example-aggregate">
  276. <title>Example of Rowset class with a new method</title>
  277. <programlisting role="php"><![CDATA[
  278. class MyBugsRowset extends Zend_Db_Table_Rowset_Abstract
  279. {
  280. /**
  281. * Find the Row in the current Rowset with the
  282. * greatest value in its 'updated_at' column.
  283. */
  284. public function getLatestUpdatedRow()
  285. {
  286. $max_updated_at = 0;
  287. $latestRow = null;
  288. foreach ($this as $row) {
  289. if ($row->updated_at > $max_updated_at) {
  290. $latestRow = $row;
  291. }
  292. }
  293. return $latestRow;
  294. }
  295. }
  296. class Bugs extends Zend_Db_Table_Abstract
  297. {
  298. protected $_name = 'bugs';
  299. protected $_rowsetClass = 'MyBugsRowset';
  300. }
  301. ]]></programlisting>
  302. </example>
  303. </sect2>
  304. </sect1>
  305. <!--
  306. vim:se ts=4 sw=4 et:
  307. -->