Home >Backend Development >PHP Tutorial >Detailed explanation of Zend_Db_Table usage in Zend Framework tutorial, zendzend_db_table_PHP tutorial
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.
<?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); ?>
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.
<?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 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.
<?php class ClassName extends Zend_Db_Table { // 默认表名为 'class_name' // 但是我们也可以对应其它表 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 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
<?php class ClassName extends Zend_Db_Table { // 默认主键为'id' // 但我们也可以设定其他列名为主键 protected $_primary = 'another_column_name'; } ?>
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.
<?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 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).
<?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 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 = ?', '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
<?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, 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.
<?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 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
<?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 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.
<?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 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.
<?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); } } ?>
Similarly, you can also set your own find() method to query data through other fields besides 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); } } ?>
更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。