Zend Framework Tutorial zend_db_table usage, zendzend_db_table_php tutorial

Source: Internet
Author: User
Tags zend framework

Zend Framework Tutorial zend_db_table usage, zendzend_db_table


Examples of this article describe zend_db_table usage. Share to everyone for your reference, as follows:

1. Introduction

Zend_db_table is the table module of the Zend framework. It connects to the database by Zend_db_adapter, checks the table object for the database schema, and operates on and queries the table.

2. Start

First, you need to zend_db_table the abstract class (Ares Note: The class is an abstract class, so you cannot instantiate directly, you can only inherit the class, and then instantiate the subclass) to set a default to the database adapter, unless you specify a different type of database adapter, otherwise, all Zend_ The Db_table class instance will use the default adapter.

<?php//build a 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.php ' for all zend_db_table objects; Zend_db_table::setdefaultadapter ($DB);? >

Next, we assume that a table named "Round_table" exists in the database. To use zend_db_table for the table, simply inherit the Zend_db_table class and create a New class. Then I can check the round_table table in the database by this class, manipulate the data row and get the result of the data.

<?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 a table name in the database ("_" needs to be added where the case is different). For example, a zend_db_table class named Sometablename in the database corresponds to the table "Some_table_ Name ". If you do not want the class name to correspond to the database table name in this underlined form, you can refactor the $_name when you define the class.

<?phpclass ClassName extends zend_db_table{  //default table name is ' class_name '  //But we can also correspond to other tables  protected $_name = ' Another_table_name ';}? >

The Zend_db_table class default field "ID" is the primary key for the table (this field is preferably self-increasing, but not required). If the table's primary key is not named "$id", you can refactor the $_primary when you define the table entity class

<?phpclass ClassName extends zend_db_table{  //Default primary key is ' ID '  //But we can also set other column names as primary key  protected $_primary = ' Another_column_name ';}? >

You can also set these variables by using the _setup () method in the table entity class, but you need to make sure that the Parent::_setup () method is executed 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. Inserting data

To insert a new row of data into a table, simply call the Insert () method with the column name: The associative array of data as the parameter.

(Zend Framework) automatically quotes the data and returns the ID value of the last row inserted
(Note: This is different from the Zend_db_adapter::insert method, which returns the number of rows inserted).

<?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. Updating data

To modify any row of data in a table, we can set a column name: An associative array of data as a parameter, call the update () method, and use a WHERE conditional clause to determine which rows need to be changed. The method modifies the data in the table and returns the number of rows that were modified.

(Zend Frameword) will automatically quote the data for modification, but this check does not include conditional clauses, so you need to use the table's Zend_db_adapter object to do the work.

Class RoundTable extends Zend_db_table {} $table = new RoundTable (), $db = $table->getadapter (); $set = array (  ' favor Ite_color ' + ' yellow ',) $where = $db->quoteinto (' first_name =? ', ' Robin '); $rows _affected = $table->update ($ Set, $where);

6. Deleting Rows

To delete data from a table, we can call the Delete () method, and use a WHERE conditional clause to determine which row to delete. The method returns the number of rows that were deleted.

(Zend Framework) does not enclose conditional clauses, so you need to use the table's Zend_db_adapter object to do the work

<?php////DELETE from round_table//   WHERE first_name = "Patsy"//class RoundTable extends zend_db_table {} $table = n EW 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 a table using a primary key value. If you only want to query a single piece of data, the method will return a Zend_db_table_row object, and when you want to query multiple records, you will return a Zend_db_table_ The rowset object.

<?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 convenient to find the data rows by the primary key, in more cases we find the data rows by the conditions of some other non-primary key. Zend_db_table provides a fetchrow () method to implement this function. We can pass a WHERE condition statement ( and an optional order statement) call the Fetchrow () method, and then Zend_db_tabel returns the Zend_db_table_row object that satisfies the first row of data for the condition.

Note that the WHERE statement will not be quoted in the (Zend Framework), 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 favor Ite_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 more than one record

If you need to retrieve more than one record at a time. You can use the Fetchall () method. Similar to using the Fetchrow () method, this method can not only set the WHERE and order clauses, but also set the Limit-count and The Limit-offset value to limit the number of results returned. After the method is executed, the selected result is returned as a Zend_db_table_rowset object.
Note that the where statement is not quoted in the (Zend Framework), so you need to process the 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 OFFSET 20$where = $db->quoteinto (' Noble_title =? ', ' Sir '); $order = ' first_name '; $count = ten; $offset =; $rowset = $table->fetchall ($where, $order, $c Ount, $offset);? >

Ten. Adding Domain Logic

As a table module of the Zend framework, zend_db_table encapsulates itself well into the unique domain logic. For example, you can overload the Insert () and update () methods to implement actions and validations before data changes are committed.

<?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 also set your own find () method to query data through 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);  }}? >

More interested in Zend related content readers can view the topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "

It is hoped that this article will help you to design PHP based on the Zend Framework framework.

Articles you may be interested in:

    • Zend Framework Framework Tutorial Zend_db_table_rowset Usage Example Analysis
    • Zend Framework Tutorial Zend_db_table_row Usage Example Analysis
    • Zend Framework Tutorial Zend_form component implement form submission and display Error prompt method
    • Introduction to Zend Framework Development Classic Tutorial
    • Zend Framework Smarty Extension Implementation method
    • Code analysis of routing mechanism of Zend framework framework
    • Zend Framework implementation of basic functions of the message book (with demo source download)
    • The Zend framework implements the method of storing the session in Memcache
    • Zend Framework Paging Class usage
    • Zend Framework implements multi-file upload function instances
    • Zend Framework Introduction Environment configuration and the first Hello World example (with demo source download)
    • Zend Framework Tutorial to connect the database and perform additions and deletions of the method (attached to the demo source download)
    • Zend Framework Tutorial Zend_db_table Table Association Instance detailed

http://www.bkjia.com/PHPjc/1113742.html www.bkjia.com true http://www.bkjia.com/PHPjc/1113742.html techarticle zend_db_table usage of the Zend Framework tutorial, Zendzend_db_table This example describes zend_db_table usage. Share to everyone for your reference, as follows: 1. About zend_db_t ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.