Zend_Db_Table_Rowset.xml 15 KB

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