Home >Backend Development >PHP Tutorial >Zend Framework tutorial - Zend_Db_Table_Rowset usage example analysis, zendframework2 example_PHP tutorial
This article describes the Zend Framework Zend_Db_Table_Rowset usage example. Share it with everyone for your reference, the details are as follows:
1. Introduction
Zend_Db_Table_Rowset is an iterator of the Zend_Db_Table_Row object collection. Generally speaking, you cannot instantiate Zend_Db_Table_Rowset yourself, but return Zend_Db_Table_Rowset as the result data by calling the Zend_Db_Table::find() method or the fetchAll() method. Next 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 object in the result set can be traversed.
<?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, that is to say, the Zend_Db_Table_Rowset object can be processed in a loop, just like using 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 properties of the object (i.e. 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(); } ?>
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.