Zend Framework Tutorial Zend_db_table_row Usage Example analysis, ZENDFRAMEWORK2 instance
This example describes the Zend_db_table_row usage of the Zend Framework tutorial. Share to everyone for your reference, as follows:
1. Introduction
Zend_db_table_row is a row data gateway for the Zend framework. In general, you cannot instantiate zend_db_table_row yourself, but by calling the Zend_db_table::find () method or Zend_db_ The Table::fetchrow () method returns the Zend_db_table_row as the result data. Once you get a Zend_db_table_row object, you can modify the record value (which is reflected as the property of the class) and then call 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 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 default Adapterrequire_once ' zend/db/table.php ' for all zend_db_table objects; Zend_db_table::setdefaultadapter ($DB);//Connect to a table in the database class RoundTable extends Zend_db_table {} $table = new RoundTable ( );? >
Next, query using the Zend_db_table::find () method and the primary key, or use the Zend_db_table::fetchrow () method.
The resulting return is a Zend_db_table_row object with the property name of Camelcaps corresponding to the underlined table name in the database.
For example, if the table name is first_name, then the modified 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 properties//that map to Table columns:////$row->id = ' 3 '//$row->nobletitle = ' Sir '//$r Ow->firstname = ' Robin '//$row->favoritecolor = ' Yellow '//?>
3. Modify the data
Modifying row data is a breeze: You only need to modify the class properties in the usual way. Then the Save () method is called to save the changed results to the data table.
<?php//connect to a table in the database class RoundTable extends Zend_db_table {} $table = new RoundTable ();//The result data retrieved from the table is a Zend_db_table_ The Row object $row = $table->fetchrow (' first_name = "Robin");////$row is now a Zend_db_table_row object with multiple public properties//that map to table columns:////$row->id = ' 3 '//$row->nobletitle = ' Sir '//$row->firstname = ' Robin '//$row->favoritecolor = ' ye Llow '////changes the favorite Color field and stores 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 change the operation, Zend_db_table_row throws an exception.
<?php//connect to a table in the database 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 of the try { $row->id = 5; echo "We should not see this message, as a exception was thrown";} catch (Zend_db_table_rowexception $e) { echo $e->getmessage ();}? >
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 Usage
- 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/1113741.html www.bkjia.com true http://www.bkjia.com/PHPjc/1113741.html techarticle Zend Framework Tutorial Zend_db_table_row Usage Example Analysis, ZENDFRAMEWORK2 example This article describes the Zend Framework tutorial Zend_db_table_row usage. Share to everyone for your reference ...