Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-13 08:43:25
Original
991 people have browsed it

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表关联实例详解
Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!