Home >php教程 >PHP开发 >Detailed explanation of Zend_Db_Table usage in Zend Framework tutorial

Detailed explanation of Zend_Db_Table usage in Zend Framework tutorial

高洛峰
高洛峰Original
2017-01-05 09:54:391396browse

The examples in this article describe the usage of Zend_Db_Table. Share it with everyone for your reference, 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 To operate and query the table.

2. To start

First you need to create 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. Then instantiate the subclass) to set a default database adapter; unless you specify other types of database adapters, all zend_db_table class instances will use the default adapter.

<?php
// 建立一个 adapter
require_once &#39;Zend/Db.php&#39;;
$params = array (
  &#39;host&#39;   => &#39;127.0.0.1&#39;,
  &#39;username&#39; => &#39;malory&#39;,
  &#39;password&#39; => &#39;******&#39;,
  &#39;dbname&#39;  => &#39;camelot&#39;
);
$db = Zend_Db::factory(&#39;PDO_MYSQL&#39;, $params);
// 为所有的Zend_Db_Table对象设定默认的adapter
require_once &#39;Zend/Db/Table.php&#39;;
Zend_Db_Table::setDefaultAdapter($db);
?>

Next, we assume that there is a database adapter A table named "round_table". To use zend_db_table for this table, just inherit the zend_db_table class and create a new class called RoundTable. Then I can use this class to check, manipulate the data rows in the round_table table in the database and get Data results.

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

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 do not want the class name to correspond to the database table name in this underlined form, you can modify $_name when defining the class. Refactoring.

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

zend_db_table class default field "id" is the primary key of the table (this field is preferably auto-incremented, but not necessary). If the primary key of the table is not named "$id ", you can reconstruct $_primary when defining the table entity class

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

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

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

4. Insert data

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

(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).

<?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(
  &#39;noble_title&#39; => &#39;King&#39;,
  &#39;first_name&#39; => &#39;Arthur&#39;,
  &#39;favorite_color&#39; => &#39;blue&#39;,
)
$id = $table->insert($data);
?>

5. Update data

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

(Zend frameword) will automatically Modify the data to be quoted, 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(
  &#39;favorite_color&#39; => &#39;yellow&#39;,
)
$where = $db->quoteInto(&#39;first_name = ?&#39;, &#39;Robin&#39;);
$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 ) will not quote the conditional clause, so you need to use the zend_db_adapter object of the table to complete the work

<?php
//
// DELETE FROM round_table
//   WHERE first_name = "Patsy"
//
class RoundTable extends Zend_Db_Table {}
$table = new RoundTable();
$db = $table->getAdapter();
$where = $db->quoteInto(&#39;first_name = ?&#39;, &#39;Patsy&#39;);
$rows_affected = $table->delete($where);
?>

7. Find data based on the primary key

By calling the find() method, Data can be easily retrieved from 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 records, a zend_db_table_rowset object will be returned.

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

Although it is very convenient to find the corresponding data row through the primary key, but more often, we find it through other non-primary key conditions. The .zend_db_table of data rows provides a fetchRow() method to implement 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 first row that satisfies the condition. The zend_db_table_row object of the data.

Note that (zend framework) will not quote the where statement, so you need to process the data through 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(&#39;noble_title = ?&#39;, &#39;Sir&#39;)
    . $db->quoteInto(&#39;AND first_name = ?&#39;, &#39;Robin&#39;);
$order = &#39;favorite_color&#39;;
$row = $table->fetchRow($where, $order);
?>

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 set limit-count and The limit-offset value is used to limit the number of returned results. After executing this method, the selected result is returned as a Zend_Db_Table_Rowset object.
Note that (zend framework) will not quote the where statement, so you need to pass zend_db_adapter performs data processing.

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

10. Adding Domain Logic

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

<?php
class RoundTable extends Zend_Db_Table
{
  public function insert($data)
  {
    // 添加一个时间戳
    if (empty($data[&#39;created_on&#39;])) {
      $data[&#39;created_on&#39;] = time();
    }
    return parent::insert($data);
  }
  public function update($data)
  {
    // 添加一个时间戳
    if (empty($data[&#39;updated_on&#39;])) {
      $data[&#39;updated_on&#39;] = time();
    }
    return parent::update($data);
  }
}
?>

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

<?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);
  }
}
?>

I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.

For more Zend Framework tutorials and detailed explanations on the usage of Zend_Db_Table, please pay attention to the PHP Chinese website for related articles!

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