Zend_Db_Table in Zend Framework tutorial
This example describes the Zend_Db_Table usage. We will share this with you 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 objects in database mode, and performs operations and queries on 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 you cannot directly implement the instance. You can only inherit this class and then instantiate the subclass; unless you specify another type of database adapter, all zend_db_table instances use the default adapter.
<? Php // create an adapterrequire_once 'zend/Db. php '; $ params = array ('host' =>' 127. 0.0.1 ', 'username' => 'malory', 'Password' => '******', 'dbname' => 'camelot '); $ db = Zend_Db: factory ('pdo _ mysql', $ params); // set the default adapterrequire_once 'zend/Db/Table for all Zend_Db_Table objects. php '; Zend_Db_Table: setdefaadapter adapter ($ db);?>
Next, let's assume that there is a table named "round_table" in the database. to use zend_db_table for this table, you only need to inherit the zend_db_table class to create a new class named RoundTable. then, I can use this class to check in the database's round_table table, operate on the Data row, and obtain the data results.
<?phpclass RoundTable extends Zend_Db_Table {}$table = new RoundTable();?>
3. Table Name and primary key
By default, the zend_db_table class treats its class name as the name of the table in the database ("_" must be added in case different "_"). for example, a zend_db_table class named SomeTableName corresponds to "some_table_name" in the database ". if you do not want to match the class name with the database table name in the form of an underscore, You Can refactor $ _ name when defining the class.
<? Phpclass 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 (This field should be self-incrementing but not required ). if the table's primary key is not named "$ id", you can refactor $ _ primary when defining the table object class.
<? Phpclass ClassName extends Zend_Db_Table {// The default primary key is 'id' // but we can also set another column name as primary key protected $ _ primary = 'another _ column_name ';}?>
You can also set these variables by using the _ setup () method in the table object class. However, make sure that you execute the parent ::_ setup () method again after the modification.
<?phpclass 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 in the table, you only need to use the column name: data associated array as the parameter and call the insert () method.
(Zend framework) automatically quotes the data and returns the id value of the last inserted row.
(Note: This is different from 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 joined array of the data as the parameter and call the update () method, A where clause is used to determine the rows to be changed. this method modifies the table data and returns the modified number of rows.
(Zend frameword) will automatically modify the data with quotation marks, but this check does not include the condition clause, 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 a table, we can call the delete () method and use a where clause to determine the row to be deleted. This method returns the number of rows to be deleted.
(Zend framework) does not apply quotation marks to condition clauses. Therefore, 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. Search for data based on the primary key
By calling the find () method, you can use the primary key value to easily retrieve data in the table. if you want to query only one piece of data, this method returns a zend_db_table_row object. If you want to query multiple records, a zend_db_table_rowset object is returned.
<?phpclass 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, in more cases, we use other non-primary key conditions to find the data rows. zend_db_table provides a fetchRow () method to implement this function. we can call the fetchRow () method through a where Condition Statement (and an optional order statement). Then, zend_db_tabel returns the zend_db_table_row object of the first row of data with full conditions.
Note that (zend framework) will not process the where statement with quotation marks, so you need to process 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('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 a time. you can use the fetchAll () method. similar to the fetchRow () method, this method can not only set the where and order clauses, but also set the limit-count and limit-offset values to limit the number of returned results. after this method is executed, the selected result is returned as a Zend_Db_Table_Rowset object.
Note that (zend framework) will not process the where statement with quotation marks, so you need to process data through zend_db_adapter.
<?phpclass 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 it well under the unique domain logic. for example, you can reload the insert () and update () Methods to perform operations and verification before submitting data changes.
<? Phpclass 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 set your own find () method to query data by other fields outside the primary key.
<?phpclass 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); }}?>