Zend_Db_Table_Rowset.xml 14 KB

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