Home > Article > Backend Development > About the usage of Zend_Db_Table_Rowset in Zend Framework
This article mainly introduces the usage of Zend_Db_Table_Rowset in the Zend Framework framework tutorial, and analyzes the related techniques of Zend_Db_Table_Rowset operation result set in the form of examples. Friends in need can refer to it
The examples of this article describe the Zend Framework framework Zend_Db_Table_Rowset usage. Share it with everyone for your reference, as follows:
1. Introduction
Zend_Db_Table_Rowset is an iterator of the Zend_Db_Table_Row object collection. Generally speaking, you cannot instantiate it yourself Zend_Db_Table_Rowset, instead, Zend_Db_Table_Rowset is returned as the result data by calling the Zend_Db_Table::find() method or the fetchAll() method. Then you can traverse the Zend_Db_Table_Row object collection and modify it.
2. Retrieve the result set
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对象设置默认 require_once 'Zend/Db/Table.php'; Zend_Db_Table::setDefaultAdapter($db); // 连接数据库表 class RoundTable extends Zend_Db_Table {} $table = new RoundTable(); ?>
Next, you can Use the Zend_Db_Table::find() method and multiple key values, or use the Zend_Db_Table::fetchAll() method to query the database.
The returned result is a Zend_Db_Table_Rowset object, through which each Zend_Db_Table_Row in the result set can be traversed Object.
<?php // 从表中取回多条记录 $rowset = $table->fetchAll(); // // $rowset现在是一个Zend_Db_Table_Rowset对象,该对象中每条记录就是一个Zend_Db_Table_Row对象 // ?>
3. Traverse the result set
Zend_Db_Table_Rowset implements the iterator interface of a simple programming language, In other words, you can loop through the Zend_Db_Table_Rowset object just like you use the foreach() function to process an array. Each value retrieved using this method is a Zend_Db_Table_Row object corresponding to the data in the table. You can view, modify and Save the attributes of the object (that is, the field values in the table.)
<?php // 连接到数据库中的表 class RoundTable extends Zend_Db_Table {} $table = new RoundTable(); // 从表中取回多条记录 $rowset = $table->fetchAll(); // 显示所有的记录 foreach ($rowset as $row) { // $row 是一个 Zend_Db_Table_Row 对象 echo "<p>" . htmlspecialchars($row->nobleTitle) . " " . htmlspecialchars($row->firstName) . "'s " . "favorite color is " . htmlspecialchars($row->favoriteColor) . ".</p>/n"; // 更新我们显示改行的次数 // (对应表中的"times_displayed"字段) $row->timesDisplayed ++; // 保存新记录. $row->save(); } ?>
The above is the entire content of this article, I hope it will be helpful to everyone Learning is helpful. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
About the operation of ZendFramework2 to connect to the database
Related knowledge about Zend Framework custom Helper class
The above is the detailed content of About the usage of Zend_Db_Table_Rowset in Zend Framework. For more information, please follow other related articles on the PHP Chinese website!