Zend_Db_Table_Row usage instance analysis in Zend Framework tutorial, zendframework2 instance
This example describes how to use Zend_Db_Table_Row In the Zend Framework tutorial. We will share this with you for your reference. The details are as follows:
1. Introduction
Zend_Db_Table_Row is the row data gateway of Zend Framework. generally, you cannot instantiate Zend_Db_Table_Row yourself. Instead, you can call the Zend_Db_Table: find () method or the Zend_Db_Table: fetchRow () method to return Zend_Db_Table_Row as the result data. once you get a Zend_Db_Table_Row object, you can modify the record value (represented by the class attribute) and call the save () method to save the changes to the original table.
2. retrieve a record
First, you need to instantiate a Zend_Db_Table class.
<? Php // set an adapterrequire_once 'zend/Db. php '; $ params = array ('host' =>' 127. 0.0.1 ', 'username' => 'malory', 'Password' => '******', 'dbname' => 'camelot '); $ db = Zend_Db: factory ('pdo _ mysql', $ params); // set the default adapterrequire_once 'zend/Db/Table for all Zend_Db_Table objects. php '; Zend_Db_Table: setdefaadapter adapter ($ db); // connect to a table in the Database class RoundTable extends Zend_Db_Table {} $ table = new RoundTable ();?>
Next, use the Zend_Db_Table: find () method and primary key for query, or use the Zend_Db_Table: fetchRow () method for query.
The returned result is a Zend_Db_Table_Row object. The attribute name of this object corresponds to the underlined table name in the database in the form of camelCaps.
For example, if the table name is first_name, the change attribute in the class is firstName.
<? Php // The result data retrieved from the table is a Zend_Db_Table_Row object $ row = $ table-> fetchRow ('first _ name = "Robin "'); //// $ row is now a Zend_Db_Table_Row object with multiple public attributes // that map to table columns: /// $ row-> id = '3' // $ row-> nobleTitle = 'Sir '// $ row-> firstName = 'Robin' // $ row-> favoriteColor = 'yellow' //?>
3. modify data
Modifying row data is easy: you only need to modify the class attributes according to the conventional method, and then call the save () method to save the changed results to the data table.
<? Php // connect to the database table class RoundTable extends Zend_Db_Table {}$ table = new RoundTable (); // The result data retrieved from the table is a Zend_Db_Table_Row object $ row = $ table-> fetchRow ('first _ name = "Robin "'); //// $ row is now a Zend_Db_Table_Row object with multiple public attributes // that map to table columns: /// $ row-> id = '3' // $ row-> nobleTitle = 'Sir '// $ row-> firstName = 'Robin' // $ row-> favoriteColor = 'yellow' // change the favorite color field, and store the changes in the data table. $ row-> favoriteColor = 'Blue'; $ row-> save ();?>
However, you cannot modify the value of the primary key. If you try to modify the value, Zend_Db_Table_Row throws an exception.
<? Php // connect to the database table class RoundTable extends Zend_Db_Table {}$ table = new RoundTable (); // fetch a record from the table as a Zend_Db_Table_Row object $ row = $ table-> fetchRow ('first _ name = "Robin "'); // We try to modify the primary key value try {$ row-> id = 5; echo "We shocould not see this message, as an exception was thrown. ";} catch (Zend_Db_Table_RowException $ e) {echo $ e-> getMessage () ;}?>