This article describes the Zend Framework tutorial Zend_Db_Table_Row usage example. Share it with everyone for your reference, the details are as follows:
1. Introduction
Zend_Db_Table_Row is the row data gateway of Zend Framework. Generally speaking, you cannot instantiate Zend_Db_Table_Row yourself, but return Zend_Db_Table_Row as the result data by calling the Zend_Db_Table::find() method or the Zend_Db_Table::fetchRow() method .Once you get a Zend_Db_Table_Row object, you can modify the record value (reflected as a class attribute) and then 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 // 设置一个 adapter require_once 'Zend/Db.php'; $params = array ( 'host' => '127.0.0.1', 'username' => 'malory', 'password' => '******', 'dbname' => 'camelot' ); $db = Zend_Db::factory('PDO_MYSQL', $params); // 为所有的Zend_Db_Table对象设置默认adapter require_once 'Zend/Db/Table.php'; Zend_Db_Table::setDefaultAdapter($db); // 连接到数据库中的某一个表 class RoundTable extends Zend_Db_Table {} $table = new RoundTable(); ?>
Next, use the Zend_Db_Table::find() method and primary key to query, or use the Zend_Db_Table::fetchRow() method to query.
The returned result is a Zend_Db_Table_Row object. The attribute name of this object is in the form of camelCaps and corresponds to the underlined table name in the database.
For example, if the table name is first_name, then the changed attribute in the class is firstName.
<?php // 从表中取回的结果数据是一个Zend_Db_Table_Row对象 $row = $table->fetchRow('first_name = "Robin"'); // // $row现在是一个带有多种公有属性的Zend_Db_Table_Row对象 // that map to table columns: // // $row->id = '3' // $row->nobleTitle = 'Sir' // $row->firstName = 'Robin' // $row->favoriteColor = 'yellow' // ?>
3. Modify data
Modifying row data is a very easy thing: you only need to modify the class attributes according to the conventional method. Then call the save() method to save the changed results to the data table.
<?php // 连接到数据库中的表 class RoundTable extends Zend_Db_Table {} $table = new RoundTable(); // 从表中取回的结果数据是一个Zend_Db_Table_Row对象 $row = $table->fetchRow('first_name = "Robin"'); // // $row现在是一个带有多种公有属性的Zend_Db_Table_Row对象 // that map to table columns: // // $row->id = '3' // $row->nobleTitle = 'Sir' // $row->firstName = 'Robin' // $row->favoriteColor = 'yellow' // // 改变favorite color字段,并且将变动存储到数据表中. $row->favoriteColor = 'blue'; $row->save(); ?>
However, you cannot modify the value of the primary key. If you attempt to modify the operation, Zend_Db_Table_Row will throw an exception.
<?php // 连接到数据库中的表 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"'); // 我们尝试修改主键值 try { $row->id = 5; echo "We should not see this message, as an exception was thrown."; } catch (Zend_Db_Table_RowException $e) { echo $e->getMessage(); } ?>
Readers who are interested in more zend-related content can check out the special topics of this site: "Zend FrameWork Framework Introductory Tutorial", "php Excellent Development Framework Summary", "Yii Framework Introduction and Summary of Common Techniques", "ThinkPHP Introductory Tutorial" , "php object-oriented programming introductory tutorial", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.