Zend_Db_Table_Row usage instance analysis in Zend Framework tutorial, zendframework2 instance

Source: Internet
Author: User

Zend_Db_Table_Row usage instance analysis in Zend Framework tutorial, zendframework2 instance

This example describes how to use Zend_Db_Table_Row In the Zend Framework tutorial. We will share this with you for your reference. The details are as follows:

1. Introduction

Zend_Db_Table_Row is the row data gateway of Zend Framework. generally, you cannot instantiate Zend_Db_Table_Row yourself. Instead, you can call the Zend_Db_Table: find () method or the Zend_Db_Table: fetchRow () method to return Zend_Db_Table_Row as the result data. once you get a Zend_Db_Table_Row object, you can modify the record value (represented by the class attribute) and call the save () method to save the changes to the original table.

2. retrieve a record

First, you need to instantiate a Zend_Db_Table class.

<? Php // set 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); // connect to a table in the Database class RoundTable extends Zend_Db_Table {} $ table = new RoundTable ();?>

Next, use the Zend_Db_Table: find () method and primary key for query, or use the Zend_Db_Table: fetchRow () method for query.
The returned result is a Zend_Db_Table_Row object. The attribute name of this object corresponds to the underlined table name in the database in the form of camelCaps.
For example, if the table name is first_name, the change attribute in the class is firstName.

<? Php // The result data retrieved from the table is a Zend_Db_Table_Row object $ row = $ table-> fetchRow ('first _ name = "Robin "'); //// $ row is now a Zend_Db_Table_Row object with multiple public attributes // that map to table columns: /// $ row-> id = '3' // $ row-> nobleTitle = 'Sir '// $ row-> firstName = 'Robin' // $ row-> favoriteColor = 'yellow' //?>

3. modify data

Modifying row data is easy: you only need to modify the class attributes according to the conventional method, and then call the save () method to save the changed results to the data table.

<? Php // connect to the database table class RoundTable extends Zend_Db_Table {}$ table = new RoundTable (); // The result data retrieved from the table is a Zend_Db_Table_Row object $ row = $ table-> fetchRow ('first _ name = "Robin "'); //// $ row is now a Zend_Db_Table_Row object with multiple public attributes // that map to table columns: /// $ row-> id = '3' // $ row-> nobleTitle = 'Sir '// $ row-> firstName = 'Robin' // $ row-> favoriteColor = 'yellow' // change the favorite color field, and store the changes in the data table. $ row-> favoriteColor = 'Blue'; $ row-> save ();?>

However, you cannot modify the value of the primary key. If you try to modify the value, Zend_Db_Table_Row throws an exception.

<? Php // connect to the database table class RoundTable extends Zend_Db_Table {}$ table = new RoundTable (); // fetch a record from the table as a Zend_Db_Table_Row object $ row = $ table-> fetchRow ('first _ name = "Robin "'); // We try to modify the primary key value try {$ row-> id = 5; echo "We shocould not see this message, as an exception was thrown. ";} catch (Zend_Db_Table_RowException $ e) {echo $ e-> getMessage () ;}?>

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.