1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.db.table.rowset">
4 <title>Zend_Db_Table_Rowset</title>
6 <sect2 id="zend.db.table.rowset.introduction">
7 <title>Introduction</title>
10 When you run a query against a Table class using the <methodname>find()</methodname> or
11 <methodname>fetchAll()</methodname> methods, the result is returned in an object of type
12 <classname>Zend_Db_Table_Rowset_Abstract</classname>. A Rowset contains a collection of
13 objects descending from <classname>Zend_Db_Table_Row_Abstract</classname>. You can
14 iterate through the Rowset and access individual Row objects, reading or modifying data
19 <sect2 id="zend.db.table.rowset.fetch">
20 <title>Fetching a Rowset</title>
23 <classname>Zend_Db_Table_Abstract</classname> provides methods
24 <methodname>find()</methodname> and <methodname>fetchAll()</methodname>, each of which
25 returns an object of type <classname>Zend_Db_Table_Rowset_Abstract</classname>.
28 <example id="zend.db.table.rowset.fetch.example">
29 <title>Example of fetching a rowset</title>
31 <programlisting language="php"><![CDATA[
33 $rowset = $bugs->fetchAll("bug_status = 'NEW'");
38 <sect2 id="zend.db.table.rowset.rows">
39 <title>Retrieving Rows from a Rowset</title>
42 The Rowset itself is usually less interesting than the Rows that it contains. This
43 section illustrates how to get the Rows that comprise the Rowset.
47 A legitimate query returns zero rows when no rows in the database match the query
48 conditions. Therefore, a Rowset object might contain zero Row objects. Since
49 <classname>Zend_Db_Table_Rowset_Abstract</classname> implements the
50 <classname>Countable</classname> interface, you can use <methodname>count()</methodname>
51 to determine the number of Rows in the Rowset.
54 <example id="zend.db.table.rowset.rows.counting.example">
55 <title>Counting the Rows in a Rowset</title>
57 <programlisting language="php"><![CDATA[
58 $rowset = $bugs->fetchAll("bug_status = 'FIXED'");
60 $rowCount = count($rowset);
63 echo "found $rowCount rows";
65 echo 'no rows matched the query';
70 <example id="zend.db.table.rowset.rows.current.example">
71 <title>Reading a Single Row from a Rowset</title>
74 The simplest way to access a Row from a Rowset is to use the
75 <methodname>current()</methodname> method. This is particularly appropriate when the
76 Rowset contains exactly one Row.
79 <programlisting language="php"><![CDATA[
81 $rowset = $bugs->fetchAll("bug_id = 1");
82 $row = $rowset->current();
87 If the Rowset contains zero rows, <methodname>current()</methodname> returns
88 <acronym>PHP</acronym>'s <constant>NULL</constant> value.
91 <example id="zend.db.table.rowset.rows.iterate.example">
92 <title>Iterating through a Rowset</title>
95 Objects descending from <classname>Zend_Db_Table_Rowset_Abstract</classname>
96 implement the <classname>SeekableIterator</classname> interface, which means you can
97 loop through them using the <methodname>foreach()</methodname> construct. Each value
98 you retrieve this way is a <classname>Zend_Db_Table_Row_Abstract</classname> object
99 that corresponds to one record from the table.
102 <programlisting language="php"><![CDATA[
105 // fetch all records from the table
106 $rowset = $bugs->fetchAll();
108 foreach ($rowset as $row) {
110 // output 'Zend_Db_Table_Row' or similar
111 echo get_class($row) . "\n";
113 // read a column in the row
114 $status = $row->bug_status;
116 // modify a column in the current row
117 $row->assigned_to = 'mmouse';
119 // write the change to the database
125 <example id="zend.db.table.rowset.rows.seek.example">
126 <title>Seeking to a known position into a Rowset</title>
129 <classname>SeekableIterator</classname> allows you to seek to a position that you
130 would like the iterator to jump to. Simply use the <methodname>seek()</methodname>
131 method for that. Pass it an integer representing the number of the Row you would
132 like your Rowset to point to next, don't forget that it starts with index 0. If the
133 index is wrong, ie doesn't exist, an exception will be thrown. You should use
134 <methodname>count()</methodname> to check the number of results before seeking to a
138 <programlisting language="php"><![CDATA[
141 // fetch all records from the table
142 $rowset = $bugs->fetchAll();
144 // takes the iterator to the 9th element (zero is one element) :
148 $row9 = $rowset->current();
151 $row9->assigned_to = 'mmouse';
157 <methodname>getRow()</methodname> allows you to get a specific row in the Rowset,
158 knowing its position; don't forget however that positions start with index zero. The
159 first parameter for <methodname>getRow()</methodname> is an integer for the position
160 asked. The second optional parameter is a boolean; it tells the Rowset iterator if
161 it must seek to that position in the same time, or not (default is
162 <constant>FALSE</constant>). This method returns a
163 <classname>Zend_Db_Table_Row</classname> object by default. If the
164 position requested does not exist, an exception will be thrown. Here is an example:
167 <programlisting language="php"><![CDATA[
170 // fetch all records from the table
171 $rowset = $bugs->fetchAll();
173 // retrieve the 9th element immediately:
177 $row9->assigned_to = 'mmouse';
182 After you have access to an individual Row object, you can manipulate the Row using
183 methods described in <link linkend="zend.db.table.row">Zend_Db_Table_Row</link>.
187 <sect2 id="zend.db.table.rowset.to-array">
188 <title>Retrieving a Rowset as an Array</title>
191 You can access all the data in the Rowset as an array using the
192 <methodname>toArray()</methodname> method of the Rowset object. This returns an array
193 containing one entry per Row. Each entry is an associative array having keys that
194 correspond to column names and elements that correspond to the respective column values.
197 <example id="zend.db.table.rowset.to-array.example">
198 <title>Using toArray()</title>
200 <programlisting language="php"><![CDATA[
202 $rowset = $bugs->fetchAll();
204 $rowsetArray = $rowset->toArray();
207 foreach ($rowsetArray as $rowArray) {
208 echo "row #$rowCount:\n";
209 foreach ($rowArray as $column => $value) {
210 echo "\t$column => $value\n";
219 The array returned from <methodname>toArray()</methodname> is not updateable. That is,
220 you can modify values in the array as you can with any array, but changes to the array
221 data are not propagated to the database.
225 <sect2 id="zend.db.table.rowset.serialize">
226 <title>Serializing and Unserializing a Rowset</title>
229 Objects of type <classname>Zend_Db_Table_Rowset_Abstract</classname> are serializable.
230 In a similar fashion to serializing an individual Row object, you can serialize a Rowset
231 and unserialize it later.
234 <example id="zend.db.table.rowset.serialize.example.serialize">
235 <title>Serializing a Rowset</title>
238 Simply use <acronym>PHP</acronym>'s <methodname>serialize()</methodname> function to
239 create a string containing a byte-stream representation of the Rowset object
243 <programlisting language="php"><![CDATA[
245 $rowset = $bugs->fetchAll();
247 // Convert object to serialized form
248 $serializedRowset = serialize($rowset);
250 // Now you can write $serializedRowset to a file, etc.
254 <example id="zend.db.table.rowset.serialize.example.unserialize">
255 <title>Unserializing a Serialized Rowset</title>
258 Use <acronym>PHP</acronym>'s <methodname>unserialize()</methodname> function to
259 restore a string containing a byte-stream representation of an object. The function
260 returns the original object.
264 Note that the Rowset object returned is in a <emphasis>disconnected</emphasis>
265 state. You can iterate through the Rowset and read the Row objects and their
266 properties, but you cannot change values in the Rows or execute other methods that
267 require a database connection (for example, queries against related tables).
270 <programlisting language="php"><![CDATA[
271 $rowsetDisconnected = unserialize($serializedRowset);
273 // Now you can use object methods and properties, but read-only
274 $row = $rowsetDisconnected->current();
275 echo $row->bug_description;
280 <title>Why do Rowsets unserialize in a disconnected state?</title>
283 A serialized object is a string that is readable to anyone who possesses it. It
284 could be a security risk to store parameters such as database account and password
285 in plain, unencrypted text in the serialized string. You would not want to store
286 such data to a text file that is not protected, or send it in an email or other
287 medium that is easily read by potential attackers. The reader of the serialized
288 object should not be able to use it to gain access to your database without knowing
294 You can reactivate a disconnected Rowset using the <methodname>setTable()</methodname>
295 method. The argument to this method is a valid object of type
296 <classname>Zend_Db_Table_Abstract</classname>, which you create. Creating a Table object
297 requires a live connection to the database, so by reassociating the Table with the
298 Rowset, the Rowset gains access to the database. Subsequently, you can change values in
299 the Row objects contained in the Rowset and save the changes to the database.
302 <example id="zend.db.table.rowset.serialize.example.set-table">
303 <title>Reactivating a Rowset as Live Data</title>
305 <programlisting language="php"><![CDATA[
306 $rowset = unserialize($serializedRowset);
310 // Reconnect the rowset to a table, and
311 // thus to a live database connection
312 $rowset->setTable($bugs);
314 $row = $rowset->current();
316 // Now you can make changes to the row and save them
317 $row->bug_status = 'FIXED';
323 Reactivating a Rowset with <methodname>setTable()</methodname> also reactivates all the
324 Row objects contained in that Rowset.
328 <sect2 id="zend.db.table.rowset.extending">
329 <title>Extending the Rowset class</title>
332 You can use an alternative concrete class for instances of Rowsets
333 by extending <classname>Zend_Db_Table_Rowset_Abstract</classname>. Specify the custom
334 Rowset class by name either in the <varname>$_rowsetClass</varname>
335 protected member of a Table class, or in the array argument of the
336 constructor of a Table object.
339 <example id="zend.db.table.rowset.extending.example">
340 <title>Specifying a custom Rowset class</title>
342 <programlisting language="php"><![CDATA[
343 class MyRowset extends Zend_Db_Table_Rowset_Abstract
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
352 protected $_name = 'products';
353 protected $_rowsetClass = 'MyRowset';
356 // Or specify a custom Rowset to be used in one
357 // instance of a Table class.
358 $bugs = new Bugs(array('rowsetClass' => 'MyRowset'));
363 Typically, the standard <classname>Zend_Db_Rowset</classname> concrete class is
364 sufficient for most usage. However, you might find it useful
365 to add new logic to a Rowset, specific to a given Table.
366 For example, a new method could calculate an aggregate
367 over all the Rows in the Rowset.
370 <example id="zend.db.table.rowset.extending.example-aggregate">
371 <title>Example of Rowset class with a new method</title>
373 <programlisting language="php"><![CDATA[
374 class MyBugsRowset extends Zend_Db_Table_Rowset_Abstract
377 * Find the Row in the current Rowset with the
378 * greatest value in its 'updated_at' column.
380 public function getLatestUpdatedRow()
384 foreach ($this as $row) {
385 if ($row->updated_at > $max_updated_at) {
393 class Bugs extends Zend_Db_Table_Abstract
395 protected $_name = 'bugs';
396 protected $_rowsetClass = 'MyBugsRowset';