Zend_Db_Table_Rowset.xml 14 KB

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