Zend Framework教程之Zend_Db_Table用法详解_php实例
本文实例讲述了Zend_Db_Table用法。分享给大家供大家参考,具体如下:
1. 简介
Zend_Db_Table 是Zend Framework的表模块.它通过zend_db_adapter连接到 数据库,为数据库模式检查表对象,并对该表进行操作和查询.
2. 开始
首先需要为抽象类zend_db_table(ares注:该类为抽象类,所以不能直接实例 化,只能先继承该类,然后实例化子类)设定一个默认对数据库adapter;除非你 指定其他类型数据库adapter,否则,所有的zend_db_table类实例都会使用 默认adapter.
<?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); ?>
接下来,我们假定数据库中存在一个名为”round_table”的表.要对该表 使用zend_db_table,只需继承zend_db_table类创建一个名为RoundTable的 新类.然后我就可以通过该类在数据库中的round_table表中检查,操作数据 行并且取得数据结果.
<?php class RoundTable extends Zend_Db_Table {} $table = new RoundTable(); ?>
3. 表名和主键
默认情况下,zend_db_table类会将其类名当作数据库中表名(大小写不同 的地方需要添加"_").例如,一个名为SomeTableName的zend_db_table类在 数据库中就对应表”some_table_name”.假如不希望将类名与数据库表名以 这种添加下划线的形式进行对应,可以在定义该类时对$_name进行重构.
<?php class ClassName extends Zend_Db_Table { // 默认表名为 'class_name' // 但是我们也可以对应其它表 protected $_name = 'another_table_name'; } ?>
zend_db_table类默认字段”id”为表的主键(该字段最好为自增的,但并不 是必须的).假如该表的主键并不是名为”$id”,你可以在定义表实体类时 对$_primary进行重构
<?php class ClassName extends Zend_Db_Table { // 默认主键为'id' // 但我们也可以设定其他列名为主键 protected $_primary = 'another_column_name'; } ?>
你也可以通过表实体类中_setup()方法设定这些变量;但是需要确保在修改 后再执行一次parent::_setup()方法.
<?php class ClassName extends Zend_Db_Table { protected function _setup() { $this->_name = 'another_table_name'; $this->_primary = 'another_column_name'; parent::_setup(); } } ?>
4. 插入数据
要在表中插入一行新数据,只需要将列名:数据的关联数组作为参数,调 用insert()方法即可.
(zend framework)会自动对数据进行加引号处理, 并返回插入的最后一行的id值
(注意:这里不同于 zend_db_adapter::insert方法,后者返回的是插入的行数).
<?php // // INSERT INTO round_table // (noble_title, first_name, favorite_color) // VALUES ("King", "Arthur", "blue") // class RoundTable extends Zend_Db_Table {} $table = new RoundTable(); $data = array( 'noble_title' => 'King', 'first_name' => 'Arthur', 'favorite_color' => 'blue', ) $id = $table->insert($data); ?>
5. 更新数据
要修改表中的任意行数据,我们可以设定一个列名:数据的关联数组作为参数,调 用update()方法,同是通过一个where条件从句来决定需要改变的行.该方法将会 修改表中数据并返回被修改的行数.
(Zend frameword)将会自动对修改对数据进行加引号处理,但是这种检查不包括 条件分句,所以你需要使用该表的zend_db_adapter对象完成该工作.
class RoundTable extends Zend_Db_Table {} $table = new RoundTable(); $db = $table->getAdapter(); $set = array( 'favorite_color' => 'yellow', ) $where = $db->quoteInto('first_name = ?', 'Robin'); $rows_affected = $table->update($set, $where);
6. Deleting Rows
要删除表中的数据,我们可以调用delete()方法,同时通过一个where条件 分句来决定需要删除的行.该方法将会返回被删除的行数.
(zend framework)不会对条件分句进行加引号处理,所以你需要使用该表 的zend_db_adapter对象完成该工作
<?php // // DELETE FROM round_table // WHERE first_name = "Patsy" // class RoundTable extends Zend_Db_Table {} $table = new RoundTable(); $db = $table->getAdapter(); $where = $db->quoteInto('first_name = ?', 'Patsy'); $rows_affected = $table->delete($where); ?>
7. 根据主键查找数据
通过调用find()方法,可以使用主键值轻松地在表中检索数据.假如你只想要查询某 一条数据,该方法将回返回一个zend_db_table_row对象,而当你想要查询多条记录时 ,将会返回一个zend_db_table_rowset对象.
<?php class RoundTable extends Zend_Db_Table {} $table = new RoundTable(); // SELECT * FROM round_table WHERE id = "1" $row = $table->find(1); // SELECT * FROM round_table WHERE id IN("1", "2", 3") $rowset = $table->find(array(1, 2, 3)); ?>
8. 取回一条记录
虽然通过主键找到相应数据行是很便利的事情,但是在更多的时候,我们是 通过其他一些非主键的条件来查找数据行的.zend_db_table提供了一个 fetchRow()方法可以实现这个功能.我们可以通过一个where条件语句(和一 个可选的order语句)调用fetchRow()方法,然后zend_db_tabel将会返回满 足条件的第一行数据的zend_db_table_row对象.
注意,(zend framework) 将不会对where语句进行加引号处理,所以你需要 通过zend_db_adapter进行数据处理
<?php // // SELECT * FROM round_table // WHERE noble_title = "Sir" // AND first_name = "Robin" // ORDER BY favorite_color // class RoundTable extends Zend_Db_Table {} $table = new RoundTable(); $db = $table->getAdapter(); $where = $db->quoteInto('noble_title = ?', 'Sir') . $db->quoteInto('AND first_name = ?', 'Robin'); $order = 'favorite_color'; $row = $table->fetchRow($where, $order); ?>
9. 取回多条记录
假如需要一次检索多条记录.可以使用fetchAll()方法.和使用fetchRow()方法类 似,该方法不仅仅可以设定where和order分句,也可以设定limit-count和 limit-offset值来限制返回的结果数.执行该方法后,把选择的结果作为一个 Zend_Db_Table_Rowset对象返回.
注意,(zend framework) 将不会对where语句进行加引号处理,所以你需要 通过zend_db_adapter进行数据处理.
<?php class RoundTable extends Zend_Db_Table {} $table = new RoundTable(); $db = $table->getAdapter(); // SELECT * FROM round_table // WHERE noble_title = "Sir" // ORDER BY first_name // LIMIT 10 OFFSET 20 $where = $db->quoteInto('noble_title = ?', 'Sir'); $order = 'first_name'; $count = 10; $offset = 20; $rowset = $table->fetchAll($where, $order, $count, $offset); ?>
10. Adding Domain Logic
作为Zend Framework的表模块,Zend_Db_Table将它自己很好的封装到独特的domain logic下. 例如,你可以重载insert()和update()方法,以实现在数据更改提交前的操作和验证.
<?php class RoundTable extends Zend_Db_Table { public function insert($data) { // 添加一个时间戳 if (empty($data['created_on'])) { $data['created_on'] = time(); } return parent::insert($data); } public function update($data) { // 添加一个时间戳 if (empty($data['updated_on'])) { $data['updated_on'] = time(); } return parent::update($data); } } ?>
类似的,你也可以设定自己的find()方法,通过主键外的其他字段来查询数据.
<?php class RoundTable extends Zend_Db_Table { public function findAllWithName($name) { $db = $this->getAdapter(); $where = $db->quoteInto("name = ?", $name); $order = "first_name"; return $this->fetchAll($where, $order); } } ?>
更多关于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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

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

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
