Home >Backend Development >PHP Tutorial >Detailed explanation of Zend_Db_Table usage in Zend Framework tutorial, zendzend_db_table_PHP tutorial

Detailed explanation of Zend_Db_Table usage in Zend Framework tutorial, zendzend_db_table_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:56:03815browse

Detailed explanation of Zend_Db_Table usage in Zend Framework tutorial, zendzend_db_table

This article describes the usage of Zend_Db_Table with examples. Share it with everyone for your reference, the details are as follows:

1. Introduction

Zend_Db_Table is the table module of Zend Framework. It connects to the database through zend_db_adapter, checks the table object for the database schema, and operates and queries the table.

2. Start

First you need to set a default database adapter for the abstract class zend_db_table (ares note: this class is an abstract class, so it cannot be instantiated directly. You can only inherit this class first and then instantiate the subclass); unless you specify other Type database adapter, otherwise, all zend_db_table class instances will use the default 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;>

Next, we assume that there is a table named "round_table" in the database. To use zend_db_table for this table, just inherit the zend_db_table class and create a new class named RoundTable. Then I can use this class in the database Check in the round_table table, operate the data rows and obtain the data results.

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

3. Table name and primary key

By default, the zend_db_table class will treat its class name as the table name in the database (you need to add "_" where the case is different). For example, a zend_db_table class named SomeTableName corresponds to the table "some_table_name" in the database ”. If you don’t want the class name to correspond to the database table name in this underlined form, you can reconstruct $_name when defining the class.

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

The default field "id" of the zend_db_table class is the primary key of the table (it is best to auto-increment this field, but it is not necessary). If the primary key of the table is not named "$id", you can define the table Refactor $_primary when creating entity classes

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

You can also set these variables through the _setup() method in the table entity class; but you need to make sure to execute the parent::_setup() method again after modification.

<&#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 data

To insert a row of new data into the table, you only need to call the insert() method with the column name: data associative array as a parameter.

(zend framework) will automatically quote the data and return the id value of the last row inserted
(Note: This is different from the zend_db_adapter::insert method, which returns the number of inserted rows).

<&#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 data

To modify any row of data in the table, we can set a column name: an associative array of data as a parameter, call the update() method, and also use a where conditional clause to determine the row that needs to be changed. This method will Will modify the data in the table and return the number of modified rows.

(Zend frameword) will automatically quote the modified data, but this check does not include conditional clauses, so you need to use the zend_db_adapter object of the table to complete the work.

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

To delete data in the table, we can call the delete() method and determine the rows that need to be deleted through a where conditional clause. This method will return the number of deleted rows.

(zend framework) does not quote conditional clauses, so you need to use the zend_db_adapter object of the table to complete the work

<&#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 data based on primary key

By calling the find() method, you can easily retrieve data in the table using the primary key value. If you only want to query a certain piece of data, this method will return a zend_db_table_row object, and when you want to query multiple When recording, a zend_db_table_rowset object will be returned.

<&#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. Retrieve a record

Although it is very convenient to find the corresponding data rows through the primary key, more often than not, we find the data rows through other non-primary key conditions. zend_db_table provides a fetchRow() method to achieve this function .We can call the fetchRow() method through a where conditional statement (and an optional order statement), and then zend_db_tabel will return the zend_db_table_row object of the first row of data that meets the condition.

Note that (zend framework) will not quote the where statement, so you need to perform data processing through 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. Retrieve multiple records

If you need to retrieve multiple records at one time, you can use the fetchAll() method. Similar to using the fetchRow() method, this method can not only set where and order clauses, but also limit-count and limit-offset value to limit the number of results returned. After executing this method, return the selected result as a Zend_Db_Table_Rowset object.
Note that (zend framework) will not quote the where statement, so you need to perform data processing through 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

As a table module of Zend Framework, Zend_Db_Table encapsulates itself well into unique domain logic. For example, you can overload the insert() and update() methods to implement operations and operations before data changes are submitted. Verify.

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

Similarly, you can also set your own find() method to query data through other fields besides the primary key.

<&#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程序设计有所帮助。

您可能感兴趣的文章:

  • Zend Framework框架教程之Zend_Db_Table_Rowset用法实例分析
  • Zend Framework教程之Zend_Db_Table_Row用法实例分析
  • 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表关联实例详解

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1113742.htmlTechArticleZend Framework教程之Zend_Db_Table用法详解,zendzend_db_table 本文实例讲述了Zend_Db_Table用法。分享给大家供大家参考,具体如下: 1. 简介 Zend_Db_T...
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