Zend_Db_Table_Rowset.xml 15 KB

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