Home  >  Article  >  Backend Development  >  About the usage of Zend_Db_Table_Rowset in Zend Framework

About the usage of Zend_Db_Table_Rowset in Zend Framework

不言
不言Original
2018-06-15 17:08:051390browse

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 &#39;Zend/Db.php&#39;;
$params = array (
  &#39;host&#39;   => &#39;127.0.0.1&#39;,
  &#39;username&#39; => &#39;malory&#39;,
  &#39;password&#39; => &#39;******&#39;,
  &#39;dbname&#39;  => &#39;camelot&#39;
);
$db = Zend_Db::factory(&#39;PDO_MYSQL&#39;, $params);
// 为所有的Zend_Db_Table对象设置默认
require_once &#39;Zend/Db/Table.php&#39;;
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) . "&#39;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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn