An example of zend_db_table_row usages of Zend Framework Tutorial _php Example

Source: Internet
Author: User
Tags zend zend framework

This example describes the Zend_db_table_row usage of the Zend Framework tutorial. Share to everyone for your reference, specific as follows:

1. Introduction

Zend_db_table_row is the Zend framework's row data gateway. Typically, you cannot instantiate the zend_db_table_row yourself, but by calling the Zend_db_table::find () method or Zend_db_ The Table::fetchrow () method returns Zend_db_table_row as the result data. Once you get a Zend_db_table_row object, you can modify the record value (embodied as a class attribute) and then call Save () method to save changes to the original table.

2. Retrieve a record

First, you need to instantiate a zend_db_table class.

<?php
//Set up a 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);
Set the default adapter
require_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, use the Zend_db_table::find () method and the primary key to query, or use the Zend_db_table::fetchrow () method query.
The resulting return result is a Zend_db_table_row object with the property name of the object in 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 a variety of public properties
//That map to Table columns:////
$row->id = ' 3 '
/ /$row->nobletitle = ' Sir '/
/$row->firstname = ' Robin '
//$row->favoritecolor = ' Yellow '
/
?>

3. Modify Data

Modifying row data is an easy thing to do: simply modify the class properties in the usual way. The Save () method is then invoked to save the changes to the datasheet.

<?php
//Connect to table class in database
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 a variety of public properties
//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 datasheet.
$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 table class in database
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 tried to modify the primary key value
try {
  $row->id = 5;
  echo "We should not" the 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 site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend Framework.

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.