Zend Framework教程之Zend_Db_Table_Row用法实例分析_PHP
本文实例讲述了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类.
<?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(); ?>
接下来,使用Zend_Db_Table::find()方法和主键进行查询,或者使 用Zend_Db_Table::fetchRow()方法查询.
得到的返回结果是一个Zend_Db_Table_Row 对象,该对象的属性名采用camelCaps的形式对应数据库中带下划线的表名.
如,表名 若为first_name,那么类中的改属性则为firstName.
<?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' // ?>
3. 修改数据
修改行数据是一件很轻松的事情:只需要按照常规的方法修改类属性.然后调用save()方法 就将改变的结果保存到了数据表中.
<?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(); ?>
但是,你不能够修改主键的值.假如你试图进行改操作, Zend_Db_Table_Row将抛出一个异常.
<?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(); } ?>
更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

.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

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.
![SCNotification has stopped working [5 steps to fix it]](https://img.php.cn/upload/article/000/887/227/168433050522031.png?x-oss-process=image/resize,m_fill,h_207,w_330)
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

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

It's been a week since we talked about the new safe mode bug affecting users who installed KB5012643 for Windows 11. This pesky issue didn't appear on the list of known issues Microsoft posted on launch day, thus catching everyone by surprise. Well, just when you thought things couldn't get any worse, Microsoft drops another bomb for users who have installed this cumulative update. Windows 11 Build 22000.652 causes more problems So the tech company is warning Windows 11 users that they may experience problems launching and using some .NET Framework 3.5 applications. Sound familiar? But please don't be surprised

How to use ACL (AccessControlList) for permission control in Zend Framework Introduction: In a web application, permission control is a crucial function. It ensures that users can only access the pages and features they are authorized to access and prevents unauthorized access. The Zend framework provides a convenient way to implement permission control, using the ACL (AccessControlList) component. This article will introduce how to use ACL in Zend Framework

PHP implementation framework: ZendFramework introductory tutorial ZendFramework is an open source website framework developed by PHP and is currently maintained by ZendTechnologies. ZendFramework adopts the MVC design pattern and provides a series of reusable code libraries to serve the implementation of Web2.0 applications and Web Serve. ZendFramework is very popular and respected by PHP developers and has a wide range of

According to news on December 9, Cooler Master recently demonstrated a mini chassis kit in cooperation with notebook modular solution provider Framework at a demonstration event at the Taipei Compute Show. The unique thing about this kit is that it can be compatible with and Install the motherboard from the framework notebook. Currently, this product has begun to be sold on the market, priced at 39 US dollars, which is equivalent to approximately 279 yuan at the current exchange rate. The model number of this chassis kit is named "frameWORKMAINBOARDCASE". In terms of design, it embodies the ultimate compactness and practicality, measuring only 297x133x15 mm. Its original design is to be able to seamlessly connect to framework notebooks
