search
Homephp教程PHP开发Zend Framework tutorial Zend_Db_Table_Row usage example analysis

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 &#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对象设置默认adapter
require_once &#39;Zend/Db/Table.php&#39;;
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(&#39;first_name = "Robin"&#39;);
//
// $row现在是一个带有多种公有属性的Zend_Db_Table_Row对象
// that map to table columns:
//
// $row->id = &#39;3&#39;
// $row->nobleTitle = &#39;Sir&#39;
// $row->firstName = &#39;Robin&#39;
// $row->favoriteColor = &#39;yellow&#39;
//
?>

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(&#39;first_name = "Robin"&#39;);
//
// $row现在是一个带有多种公有属性的Zend_Db_Table_Row对象
// that map to table columns:
//
// $row->id = &#39;3&#39;
// $row->nobleTitle = &#39;Sir&#39;
// $row->firstName = &#39;Robin&#39;
// $row->favoriteColor = &#39;yellow&#39;
//
// 改变favorite color字段,并且将变动存储到数据表中.
$row->favoriteColor = &#39;blue&#39;;
$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(&#39;first_name = "Robin"&#39;);
// 我们尝试修改主键值
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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function