Zend_Db_Table_RowsetIntroductionWhen you run a query against a Table class using the find() or fetchAll() methods, the result is returned in an object of type Zend_Db_Table_Rowset_Abstract. A Rowset contains a collection of objects descending from Zend_Db_Table_Row_Abstract. You can iterate through the Rowset and access individual Row objects, reading or modifying data in the Rows. Fetching a RowsetZend_Db_Table_Abstract provides methods find() and fetchAll(), each of which returns an object of type Zend_Db_Table_Rowset_Abstract. Example #1 Example of fetching a rowset
Retrieving Rows from a RowsetThe Rowset itself is usually less interesting than the Rows that it contains. This section illustrates how to get the Rows that comprise the Rowset.
A legitimate query returns zero rows when no rows in the database match the query
conditions. Therefore, a Rowset object might contain zero Row objects. Since
Zend_Db_Table_Rowset_Abstract implements the
Example #2 Counting the Rows in a Rowset Example #3 Reading a Single Row from a Rowset The simplest way to access a Row from a Rowset is to use the current() method. This is particularly appropriate when the Rowset contains exactly one Row.
If the Rowset contains zero rows, current() returns PHP's NULL value. Example #4 Iterating through a Rowset
Objects descending from Zend_Db_Table_Rowset_Abstract
implement the
Example #5 Seeking to a known position into a Rowset
getRow() allows you to get a specific row in the Rowset, knowing its position; don't forget however that positions start with index zero. The first parameter for getRow() is an integer for the position asked. The second optional parameter is a boolean; it tells the Rowset iterator if it must seek to that position in the same time, or not (default is false). This method returns a Zend_Db_Table_Row object by default. If the position requested does not exist, an exception will be thrown. Here is an example :
After you have access to an individual Row object, you can manipulate the Row using methods described in Zend_Db_Table_Row. Retrieving a Rowset as an ArrayYou can access all the data in the Rowset as an array using the toArray() method of the Rowset object. This returns an array containing one entry per Row. Each entry is an associative array having keys that correspond to column names and elements that correspond to the respective column values. Example #6 Using toArray() The array returned from toArray() is not updateable. That is, you can modify values in the array as you can with any array, but changes to the array data are not propagated to the database. Serializing and Unserializing a RowsetObjects of type Zend_Db_Table_Rowset_Abstract are serializable. In a similar fashion to serializing an individual Row object, you can serialize a Rowset and unserialize it later. Example #7 Serializing a Rowset Simply use PHP's serialize() function to create a string containing a byte-stream representation of the Rowset object argument.
Example #8 Unserializing a Serialized Rowset Use PHP's unserialize() function to restore a string containing a byte-stream representation of an object. The function returns the original object. Note that the Rowset object returned is in a disconnected state. You can iterate through the Rowset and read the Row objects and their properties, but you cannot change values in the Rows or execute other methods that require a database connection (for example, queries against related tables).
You can reactivate a disconnected Rowset using the setTable() method. The argument to this method is a valid object of type Zend_Db_Table_Abstract, which you create. Creating a Table object requires a live connection to the database, so by reassociating the Table with the Rowset, the Rowset gains access to the database. Subsequently, you can change values in the Row objects contained in the Rowset and save the changes to the database. Example #9 Reactivating a Rowset as Live Data
Reactivating a Rowset with setTable() also reactivates all the Row objects contained in that Rowset. Extending the Rowset classYou can use an alternative concrete class for instances of Rowsets by extending Zend_Db_Table_Rowset_Abstract. Specify the custom Rowset class by name either in the $_rowsetClass protected member of a Table class, or in the array argument of the constructor of a Table object. Example #10 Specifying a custom Rowset class
Typically, the standard Zend_Db_Rowset concrete class is sufficient for most usage. However, you might find it useful to add new logic to a Rowset, specific to a given Table. For example, a new method could calculate an aggregate over all the Rows in the Rowset. Example #11 Example of Rowset class with a new method
|