Table of Contents
Zend Framework教程之Zend_Db_Table_Row用法实例分析,zendframework2实例
您可能感兴趣的文章:
Home php教程 php手册 Zend Framework教程之Zend_Db_Table_Row用法实例分析,zendframework2实例

Zend Framework教程之Zend_Db_Table_Row用法实例分析,zendframework2实例

Jun 13, 2016 am 08:43 AM
db framework table zend

Zend Framework教程之Zend_Db_Table_Row用法实例分析,zendframework2实例

本文实例讲述了Zend Framework教程之Zend_Db_Table_Row用法。分享给大家供大家参考,具体如下:

1. 简介

Zend_Db_Table_Row是Zend Framework的行数据网关.通常来说,你不可以自己实例化Zend_Db_Table_Row, 而是通过调用Zend_Db_Table::find()方法或者Zend_Db_Table::fetchRow()方法将Zend_Db_Table_Row作为 结果数据返回过来.一旦你得到来一个Zend_Db_Table_Row对象,你可以修改记录值(体现为类的属性)然后 调用save()方法可以将更改保存到原表上.

2. 取回一条记录

首先,需要实例化一个Zend_Db_Table类.

<&#63;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对象设置默认adapter
require_once 'Zend/Db/Table.php';
Zend_Db_Table::setDefaultAdapter($db);
// 连接到数据库中的某一个表
class RoundTable extends Zend_Db_Table {}
$table = new RoundTable();
&#63;>

Copy after login

接下来,使用Zend_Db_Table::find()方法和主键进行查询,或者使 用Zend_Db_Table::fetchRow()方法查询.
得到的返回结果是一个Zend_Db_Table_Row 对象,该对象的属性名采用camelCaps的形式对应数据库中带下划线的表名.
如,表名 若为first_name,那么类中的改属性则为firstName.

<&#63;php
// 从表中取回的结果数据是一个Zend_Db_Table_Row对象
$row = $table->fetchRow('first_name = "Robin"');
//
// $row现在是一个带有多种公有属性的Zend_Db_Table_Row对象
// that map to table columns:
//
// $row->id = '3'
// $row->nobleTitle = 'Sir'
// $row->firstName = 'Robin'
// $row->favoriteColor = 'yellow'
//
&#63;>

Copy after login

3. 修改数据

修改行数据是一件很轻松的事情:只需要按照常规的方法修改类属性.然后调用save()方法 就将改变的结果保存到了数据表中.

<&#63;php
// 连接到数据库中的表
class RoundTable extends Zend_Db_Table {}
$table = new RoundTable();
// 从表中取回的结果数据是一个Zend_Db_Table_Row对象
$row = $table->fetchRow('first_name = "Robin"');
//
// $row现在是一个带有多种公有属性的Zend_Db_Table_Row对象
// that map to table columns:
//
// $row->id = '3'
// $row->nobleTitle = 'Sir'
// $row->firstName = 'Robin'
// $row->favoriteColor = 'yellow'
//
// 改变favorite color字段,并且将变动存储到数据表中.
$row->favoriteColor = 'blue';
$row->save();
&#63;>

Copy after login

但是,你不能够修改主键的值.假如你试图进行改操作, Zend_Db_Table_Row将抛出一个异常.

<&#63;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('first_name = "Robin"');
// 我们尝试修改主键值
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();
}
&#63;>

Copy after login

更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • Zend Framework框架教程之Zend_Db_Table_Rowset用法实例分析
  • Zend Framework教程之Zend_Db_Table用法详解
  • Zend Framework教程之Zend_Form组件实现表单提交并显示错误提示的方法
  • Zend Framework开发入门经典教程
  • Zend Framework框架Smarty扩展实现方法
  • Zend Framework框架路由机制代码分析
  • Zend Framework实现具有基本功能的留言本(附demo源码下载)
  • Zend Framework实现将session存储在memcache中的方法
  • Zend Framework分页类用法详解
  • Zend Framework实现多文件上传功能实例
  • Zend Framework入门之环境配置及第一个Hello World示例(附demo源码下载)
  • Zend Framework教程之连接数据库并执行增删查的方法(附demo源码下载)
  • Zend Framework教程之Zend_Db_Table表关联实例详解
Statement of this Website
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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Microsoft NET Framework Installation Issues Error Code 0x800c0006 Fix Microsoft NET Framework Installation Issues Error Code 0x800c0006 Fix May 05, 2023 pm 04:01 PM

.NET Framework 4 is required by developers and end users to run the latest versions of applications on Windows. However, while downloading and installing .NET Framework 4, many users complained that the installer stopped midway, displaying the following error message - " .NET Framework 4 has not been installed because Download failed with error code 0x800c0006 ". If you are also experiencing it while installing .NETFramework4 on your device then you are at the right place

Comparative analysis of Oracle and DB2 database technology Comparative analysis of Oracle and DB2 database technology Mar 11, 2024 am 09:54 AM

Oracle and DB2 are two well-known relational database management systems (RDBMS) that are widely used in enterprise applications. In this article, we will compare the two database technologies of Oracle and DB2 and analyze them in detail, including analysis of their characteristics, performance, functions and usage examples. 1. Overview of Oracle database technology Oracle is a relational database management system developed by Oracle Corporation of the United States. It is widely used in enterprise-level applications and has strong performance and stability.

How to identify Windows upgrade issues using SetupDiag on Windows 11/10 How to identify Windows upgrade issues using SetupDiag on Windows 11/10 Apr 17, 2023 am 10:07 AM

Whenever your Windows 11 or Windows 10 PC has an upgrade or update issue, you will usually see an error code indicating the actual reason behind the failure. However, sometimes confusion can arise when an upgrade or update fails without an error code being displayed. With handy error codes, you know exactly where the problem is so you can try to fix it. But since no error code appears, it becomes challenging to identify the issue and resolve it. This will take up a lot of your time to simply find out the reason behind the error. In this case, you can try using a dedicated tool called SetupDiag provided by Microsoft that helps you easily identify the real reason behind the error.

Comparison and differences of SQL syntax between Oracle and DB2 Comparison and differences of SQL syntax between Oracle and DB2 Mar 11, 2024 pm 12:09 PM

Oracle and DB2 are two commonly used relational database management systems, each of which has its own unique SQL syntax and characteristics. This article will compare and differ between the SQL syntax of Oracle and DB2, and provide specific code examples. Database connection In Oracle, use the following statement to connect to the database: CONNECTusername/password@database. In DB2, the statement to connect to the database is as follows: CONNECTTOdataba

SCNotification has stopped working [5 steps to fix it] SCNotification has stopped working [5 steps to fix it] May 17, 2023 pm 09:35 PM

As a Windows user, you are likely to encounter SCNotification has stopped working error every time you start your computer. SCNotification.exe is a Microsoft system notification file that crashes every time you start your PC due to permission errors and network failures. This error is also known by its problematic event name. So you might not see this as SCNotification having stopped working, but as bug clr20r3. In this article, we will explore all the steps you need to take to fix SCNotification has stopped working so that it doesn’t bother you again. What is SCNotification.e

Comparative analysis of Oracle and DB2 database performance Comparative analysis of Oracle and DB2 database performance Mar 09, 2024 pm 10:00 PM

Oracle and DB2 databases are two leading relational database management systems that are widely used in enterprise-level applications. In practical applications, database performance is often one of the important indicators for evaluating the quality of a database system. This article will conduct a comparative analysis of the performance of Oracle and DB2 databases, and use specific code examples to demonstrate the differences between them. 1. Oracle database performance analysis Oracle database is a powerful database management system with good scalability and stability.

What file format is db? What file format is db? May 19, 2021 am 11:56 AM

db is the abbreviation of "datebase", which is a format of "database file". It is a file used by software to store data, which is equivalent to a database. Each software has its own storage format. For example, "Thumbs.db" under Win7 system is a thumbnail data file, so the db file is not a specific file format.

Microsoft .NET Framework 4.5.2, 4.6, and 4.6.1 will end support in April 2022 Microsoft .NET Framework 4.5.2, 4.6, and 4.6.1 will end support in April 2022 Apr 17, 2023 pm 02:25 PM

Microsoft Windows users who have installed Microsoft.NET version 4.5.2, 4.6, or 4.6.1 must install a newer version of the Microsoft Framework if they want Microsoft to support the framework through future product updates. According to Microsoft, all three frameworks will cease support on April 26, 2022. After the support date ends, the product will not receive "security fixes or technical support." Most home devices are kept up to date through Windows updates. These devices already have newer versions of frameworks installed, such as .NET Framework 4.8. Devices that are not updating automatically may

See all articles