Home >php教程 >PHP开发 >Zend Framework tutorial - Zend_Db_Table_Rowset usage example analysis

Zend Framework tutorial - Zend_Db_Table_Rowset usage example analysis

高洛峰
高洛峰Original
2017-01-05 09:44:271149browse

The example in this article describes the usage of Zend Framework Zend_Db_Table_Rowset. 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 Zend_Db_Table_Rowset yourself, but by calling The Zend_Db_Table::find() method or the fetchAll() method returns Zend_Db_Table_Rowset as the result data. 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 &#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 object in the result set can be traversed.

<?php
// 从表中取回多条记录
$rowset = $table->fetchAll();
//
// $rowset现在是一个Zend_Db_Table_Rowset对象,该对象中每条记录就是一个Zend_Db_Table_Row对象
//
?>

3. Traversing the result set

Zend_Db_Table_Rowset implements a simple programming language Iterator interface of You can view, modify and save the properties 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();
}
?>

I hope this article will be helpful to everyone's PHP programming based on the Zend Framework framework.

For more Zend Framework framework tutorials and Zend_Db_Table_Rowset usage example analysis related articles, please pay attention to 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