This article describes the usage of Zend_Db_Table. Share it for your reference, as follows:
1. Introduction
Zend_Db_Table is a table module of Zend Framework. It connects to the database through zend_db_adapter, checks table objects for 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 the class first and then instantiate the subclass). Unless you specify another type of database adapter, all zend_db_table class instances will use the default adapter.
<?php // Create an adapterrequire_once 'Zend/'; $params = array ( 'host' => '127.0.0.1', 'username' => 'malory', 'password' => '******', 'dbname' => 'camelot' ); $db = Zend_Db::factory('PDO_MYSQL', $params); // Set default adapters for all Zend_Db_Table objectsrequire_once 'Zend/Db/'; Zend_Db_Table::setDefaultAdapter($db); ?>
Next, we assume that there is a table named "round_table" in the database. To use zend_db_table for this table, you just inherit the zend_db_table class and create a new class named RoundTable. Then I can check the round_table table in the database through this class, manipulate the data rows and get the 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 (the "_" needs to be added if 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 form of underscore, you can refactor $_name when defining the class.
<?php class ClassName extends Zend_Db_Table { // The default table name is 'class_name' // But we can also correspond to other tables protected $_name = 'another_table_name'; } ?>
The default field "id" of the zend_db_table class is the primary key of the table (it is best to be self-incremented, but it is not necessary). If the primary key of the table is not named "$id", you can refactor $_primary when defining the table entity class.
<?php class ClassName extends Zend_Db_Table { // The default primary key is 'id' // But we can also set other column names as primary keys protected $_primary = 'another_column_name'; } ?>
You can also set these variables through the _setup() method in the table entity class; however, you need to make sure that the parent::_setup() method is executed again after modification.
<?php class ClassName extends Zend_Db_Table { protected function _setup() { $this->_name = 'another_table_name'; $this->_primary = 'another_column_name'; parent::_setup(); } } ?>
4. Insert data
To insert a new row of data into the table, you only need to use the associative array of the column name: data as a parameter and call the insert() method.
(zend framework) will automatically add quotation marks to the data and return the id value of the last line 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( 'noble_title' => 'King', 'first_name' => 'Arthur', 'favorite_color' => 'blue', ) $id = $table->insert($data); ?>
5. Update data
To modify any row data in the table, we can set a column name: the data associative array as a parameter, and call the update() method, which also determines the rows that need to be changed through a where condition clause. This method will modify the data in the table and return the number of rows that are modified.
(Zend framework) will automatically quote the modifications, 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 = ?', 'Robin'); $rows_affected = $table->update($set, $where);
6. Deleting Rows
To delete data in the table, we can call the delete() method and use a where condition clause to determine the rows that need to be deleted. This method will return the number of rows that have been deleted.
(zend framework) does not quotation marks on conditional clauses, so you need to use the zend_db_adapter object of the table to do 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('first_name = ?', 'Patsy'); $rows_affected = $table->delete($where); ?>
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, the method will return a zend_db_table_row object, and when you want to query multiple records, it will return a zend_db_table_rowset object.
<?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, in more cases, we use other non-primary key conditions to find the data row.zend_db_table provides a fetchRow() method to achieve this function. We can call the fetchRow() method through a where condition 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 use quotes to the where statement, so you need to use zend_db_adapter for data processing.
<?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. Retrieve multiple records
If you need to retrieve multiple records at once, 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 limit-offset values 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 use quotes to the where statement, so you need to use zend_db_adapter for 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('noble_title = ?', 'Sir'); $order = 'first_name'; $count = 10; $offset = 20; $rowset = $table->fetchAll($where, $order, $count, $offset); ?>
10. Adding Domain Logic
As the table module of Zend Framework, Zend_Db_Table encapsulates itself well into a unique domain logic. For example, you can overload the 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) { // Add a timestamp if (empty($data['created_on'])) { $data['created_on'] = time(); } return parent::insert($data); } public function update($data) { // Add a timestamp if (empty($data['updated_on'])) { $data['updated_on'] = time(); } return parent::update($data); } } ?>
Similarly, you can also set your own find() method to query data through other fields outside the primary key.
<?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); } } ?>
For more information about Zend, please visit the special topic of this site:Zend FrameWork Framework Introduction Tutorial》、《Summary of excellent development framework for php》、《Yii framework introduction and common techniques summary》、《ThinkPHP Introduction Tutorial》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope that this article will be helpful to everyone's PHP programming based on the Zend Framework framework.