search
HomeBackend DevelopmentPHP TutorialZend 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.

<&#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);
&#63;>

接下来,我们假定数据库中存在一个名为”round_table”的表.要对该表 使用zend_db_table,只需继承zend_db_table类创建一个名为RoundTable的 新类.然后我就可以通过该类在数据库中的round_table表中检查,操作数据 行并且取得数据结果.

<&#63;php
class RoundTable extends Zend_Db_Table {}
$table = new RoundTable();
&#63;>

3. 表名和主键

默认情况下,zend_db_table类会将其类名当作数据库中表名(大小写不同 的地方需要添加"_").例如,一个名为SomeTableName的zend_db_table类在 数据库中就对应表”some_table_name”.假如不希望将类名与数据库表名以 这种添加下划线的形式进行对应,可以在定义该类时对$_name进行重构.

<&#63;php
class ClassName extends Zend_Db_Table
{
  // 默认表名为 'class_name'
  // 但是我们也可以对应其它表
  protected $_name = 'another_table_name';
}
&#63;>

zend_db_table类默认字段”id”为表的主键(该字段最好为自增的,但并不 是必须的).假如该表的主键并不是名为”$id”,你可以在定义表实体类时 对$_primary进行重构

<&#63;php
class ClassName extends Zend_Db_Table
{
  // 默认主键为'id'
  // 但我们也可以设定其他列名为主键
  protected $_primary = 'another_column_name';
}
&#63;>

你也可以通过表实体类中_setup()方法设定这些变量;但是需要确保在修改 后再执行一次parent::_setup()方法.

<&#63;php
class ClassName extends Zend_Db_Table
{
  protected function _setup()
  {
    $this->_name = 'another_table_name';
    $this->_primary = 'another_column_name';
    parent::_setup();
  }
}
&#63;>

4. 插入数据

要在表中插入一行新数据,只需要将列名:数据的关联数组作为参数,调 用insert()方法即可.

(zend framework)会自动对数据进行加引号处理, 并返回插入的最后一行的id值
(注意:这里不同于 zend_db_adapter::insert方法,后者返回的是插入的行数).

<&#63;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);
&#63;>

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 = &#63;', 'Robin');
$rows_affected = $table->update($set, $where);

6. Deleting Rows

要删除表中的数据,我们可以调用delete()方法,同时通过一个where条件 分句来决定需要删除的行.该方法将会返回被删除的行数.

(zend framework)不会对条件分句进行加引号处理,所以你需要使用该表 的zend_db_adapter对象完成该工作

<&#63;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 = &#63;', 'Patsy');
$rows_affected = $table->delete($where);
&#63;>

7. 根据主键查找数据

通过调用find()方法,可以使用主键值轻松地在表中检索数据.假如你只想要查询某 一条数据,该方法将回返回一个zend_db_table_row对象,而当你想要查询多条记录时 ,将会返回一个zend_db_table_rowset对象.

<&#63;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));
&#63;>

8. 取回一条记录

虽然通过主键找到相应数据行是很便利的事情,但是在更多的时候,我们是 通过其他一些非主键的条件来查找数据行的.zend_db_table提供了一个 fetchRow()方法可以实现这个功能.我们可以通过一个where条件语句(和一 个可选的order语句)调用fetchRow()方法,然后zend_db_tabel将会返回满 足条件的第一行数据的zend_db_table_row对象.

注意,(zend framework) 将不会对where语句进行加引号处理,所以你需要 通过zend_db_adapter进行数据处理

<&#63;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 = &#63;', 'Sir')
    . $db->quoteInto('AND first_name = &#63;', 'Robin');
$order = 'favorite_color';
$row = $table->fetchRow($where, $order);
&#63;>

9. 取回多条记录

假如需要一次检索多条记录.可以使用fetchAll()方法.和使用fetchRow()方法类 似,该方法不仅仅可以设定where和order分句,也可以设定limit-count和 limit-offset值来限制返回的结果数.执行该方法后,把选择的结果作为一个 Zend_Db_Table_Rowset对象返回.
注意,(zend framework) 将不会对where语句进行加引号处理,所以你需要 通过zend_db_adapter进行数据处理.

<&#63;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 = &#63;', 'Sir');
$order = 'first_name';
$count = 10;
$offset = 20;
$rowset = $table->fetchAll($where, $order, $count, $offset);
&#63;>

10. Adding Domain Logic

作为Zend Framework的表模块,Zend_Db_Table将它自己很好的封装到独特的domain logic下. 例如,你可以重载insert()和update()方法,以实现在数据更改提交前的操作和验证.

<&#63;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);
  }
}
&#63;>

类似的,你也可以设定自己的find()方法,通过主键外的其他字段来查询数据.

<&#63;php
class RoundTable extends Zend_Db_Table
{
  public function findAllWithName($name)
  {
    $db = $this->getAdapter();
    $where = $db->quoteInto("name = &#63;", $name);
    $order = "first_name";
    return $this->fetchAll($where, $order);
  }
}
&#63;>

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

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

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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

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.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)