The example in this article describes the usage of Zend_Db_Table_Row in the Zend Framework tutorial. Share it with everyone for your reference, 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 call The Zend_Db_Table::find() method or Zend_Db_Table::fetchRow() method returns Zend_Db_Table_Row as the result data. 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. 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 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, and the attribute name of the object is in the form of camelCaps corresponding to the underlined table name in the database.
For example, if the table name is first_name, then the modified 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: just need 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 try to perform a modification operation, Zend_Db_Table_Row An exception will be thrown.
<?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(); } ?>
I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.
For more Zend Framework tutorial Zend_Db_Table_Row usage example analysis and related articles, please pay attention to the PHP Chinese website!