Zend_Db_Table_RowIntroductionZend_Db_Table_Row is a class that contains an individual row of a Zend_Db_Table object. When you run a query against a Table class, the result is returned in a set of Zend_Db_Table_Row objects. You can also use this object to create new rows and add them to the database table. Zend_Db_Table_Row is an implementation of the » Row Data Gateway pattern. Fetching a RowZend_Db_Table_Abstract provides methods find() and fetchAll(), which each return an object of type Zend_Db_Table_Rowset, and the method fetchRow(), which returns an object of type Zend_Db_Table_Row. Example #1 Example of fetching a row
A Zend_Db_Table_Rowset object contains a collection of Zend_Db_Table_Row objects. See the chapter about table rowset for details. Example #2 Example of reading a row in a rowset
Reading column values from a rowZend_Db_Table_Row_Abstract provides accessor methods so you can reference columns in the row as object properties. Example #3 Example of reading a column in a row
Retrieving Row Data as an ArrayYou can access the row's data as an array using the toArray() method of the Row object. This returns an associative array of the column names to the column values. Example #4 Example of using the toArray() method
The array returned from toArray() is not updateable. You can modify values in the array as you can with any array, but you cannot save changes to this array to the database directly. Fetching data from related tablesThe Zend_Db_Table_Row_Abstract class provides methods for fetching rows and rowsets from related tables. See the relationship chapter for more information on table relationships. Writing rows to the databaseChanging column values in a rowYou can set individual column values using column accessors, similar to how the columns are read as object properties in the example above. Using a column accessor to set a value changes the column value of the row object in your application, but it does not commit the change to the database yet. You can do that with the save() method. Example #5 Example of changing a column in a row
Inserting a new rowYou can create a new row for a given table with the createRow() method of the table class. You can access fields of this row with the object-oriented interface, but the row is not stored in the database until you call the save() method. Example #6 Example of creating a new row for a table
The optional argument to the createRow() method is an associative array, with which you can populate fields of the new row. Example #7 Example of populating a new row for a table
Changing values in multiple columnsZend_Db_Table_Row_Abstract provides the setFromArray() method to enable you to set several columns in a single row at once, specified in an associative array that maps the column names to values. You may find this method convenient for setting values both for new rows and for rows you need to update. Example #8 Example of using setFromArray() to set values in a new Row
Deleting a rowYou can call the delete() method on a Row object. This deletes rows in the database matching the primary key in the Row object. Example #9 Example of deleting a row
You do not have to call save() to apply the delete; it is executed against the database immediately. Serializing and unserializing rowsIt is often convenient to save the contents of a database row to be used later. Serialization is the name for the operation that converts an object into a form that is easy to save in offline storage (for example, a file). Objects of type Zend_Db_Table_Row_Abstract are serializable. Serializing a RowSimply use PHP's serialize() function to create a string containing a byte-stream representation of the Row object argument. Example #10 Example of serializing a row
Unserializing Row DataUse 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 Row object returned is in a disconnected state. You can read the Row object and its properties, but you cannot change values in the Row or execute other methods that require a database connection (for example, queries against related tables). Example #11 Example of unserializing a serialized row
Reactivating a Row as Live DataYou can reactivate a disconnected Row, 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 Row, the Row gains access to the database. Subsequently, you can change values in the Row object and save the changes to the database. Example #12 Example of reactivating a row
Extending the Row classZend_Db_Table_Row is the default concrete class that extends Zend_Db_Table_Row_Abstract. You can define your own concrete class for instances of Row by extending Zend_Db_Table_Row_Abstract. To use your new Row class to store results of Table queries, specify the custom Row class by name either in the $_rowClass protected member of a Table class, or in the array argument of the constructor of a Table object. Example #13 Specifying a custom Row class
Row initializationIf application-specific logic needs to be initialized when a row is constructed, you can select to move your tasks to the init() method, which is called after all row metadata has been processed. This is recommended over the __construct() method if you do not need to alter the metadata in any programmatic way. Example #14 Example usage of init() method Defining Custom Logic for Insert, Update, and Delete in Zend_Db_Table_RowThe Row class calls protected methods _insert(), _update(), and _delete() before performing the corresponding operations INSERT, UPDATE, and DELETE. You can add logic to these methods in your custom Row subclass. If you need to do custom logic in a specific table, and the custom logic must occur for every operation on that table, it may make more sense to implement your custom code in the insert(), update() and delete() methods of your Table class. However, sometimes it may be necessary to do custom logic in the Row class. Below are some example cases where it might make sense to implement custom logic in a Row class instead of in the Table class: Example #15 Example of custom logic in a Row class The custom logic may not apply in all cases of operations on the respective Table. You can provide custom logic on demand by implementing it in a Row class and creating an instance of the Table class with that custom Row class specified. Otherwise, the Table uses the default Row class. You need data operations on this table to record the operation to a Zend_Log object, but only if the application configuration has enabled this behavior.
Example #16 Example of a Row class that logs insert data for multiple tables The custom logic may be common to multiple tables. Instead of implementing the same custom logic in every one of your Table classes, you can implement the code for such actions in the definition of a Row class, and use this Row in each of your Table classes. In this example, the logging code is identical in all table classes.
Define Inflection in Zend_Db_Table_RowSome people prefer that the table class name match a table name in the RDBMS by using a string transformation called inflection. Zend_Db classes do not implement inflection by default. See the chapter about extending inflection for an explanation of this policy. If you prefer to use inflection, then you must implement the transformation yourself, by overriding the _transformColumn() method in a custom Row class, and using that custom Row class when you perform queries against your Table class. Example #17 Example of defining an inflection transformation This allows you to use an inflected version of the column name in the accessors. The Row class uses the _transformColumn() method to change the name you use to the native column name in the database table.
You are responsible for writing the functions to perform inflection transformation. Zend Framework does not provide such a function.
|