Zend_Db_Table_Rowset.xml 15 KB

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