2
0

Zend_Db_Table_Rowset.xml 14 KB

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