Home > Backend Development > PHP Tutorial > Zend Framework tutorial Zend_Db_Table_Row usage example analysis, zendframework2 example_PHP tutorial

Zend Framework tutorial Zend_Db_Table_Row usage example analysis, zendframework2 example_PHP tutorial

WBOY
Release: 2016-07-12 08:56:04
Original
848 people have browsed it

Zend Framework tutorial Zend_Db_Table_Row usage example analysis, zendframework2 example

This article describes the Zend Framework tutorial Zend_Db_Table_Row usage example. Share it with everyone for your reference, the details are as follows:

1. Introduction

Zend_Db_Table_Row is the row data gateway of Zend Framework. Generally speaking, you cannot instantiate Zend_Db_Table_Row yourself, but return Zend_Db_Table_Row as the result data by calling the Zend_Db_Table::find() method or the Zend_Db_Table::fetchRow() method .Once you get a Zend_Db_Table_Row object, you can modify the record value (reflected as a class attribute) and then 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.

<&#63;php
// 设置一个 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);
// 为所有的Zend_Db_Table对象设置默认adapter
require_once 'Zend/Db/Table.php';
Zend_Db_Table::setDefaultAdapter($db);
// 连接到数据库中的某一个表
class RoundTable extends Zend_Db_Table {}
$table = new RoundTable();
&#63;>

Copy after login

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

<&#63;php
// 从表中取回的结果数据是一个Zend_Db_Table_Row对象
$row = $table->fetchRow('first_name = "Robin"');
//
// $row现在是一个带有多种公有属性的Zend_Db_Table_Row对象
// that map to table columns:
//
// $row->id = '3'
// $row->nobleTitle = 'Sir'
// $row->firstName = 'Robin'
// $row->favoriteColor = 'yellow'
//
&#63;>

Copy after login

3. Modify data

Modifying row data is a very easy thing: you only need to modify the class attributes according to the conventional method. Then call the save() method to save the changed results to the data table.

<&#63;php
// 连接到数据库中的表
class RoundTable extends Zend_Db_Table {}
$table = new RoundTable();
// 从表中取回的结果数据是一个Zend_Db_Table_Row对象
$row = $table->fetchRow('first_name = "Robin"');
//
// $row现在是一个带有多种公有属性的Zend_Db_Table_Row对象
// that map to table columns:
//
// $row->id = '3'
// $row->nobleTitle = 'Sir'
// $row->firstName = 'Robin'
// $row->favoriteColor = 'yellow'
//
// 改变favorite color字段,并且将变动存储到数据表中.
$row->favoriteColor = 'blue';
$row->save();
&#63;>

Copy after login

However, you cannot modify the value of the primary key. If you attempt to modify the operation, Zend_Db_Table_Row will throw an exception.

<&#63;php
// 连接到数据库中的表
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"');
// 我们尝试修改主键值
try {
  $row->id = 5;
  echo "We should not see this message, as an exception was thrown.";
} catch (Zend_Db_Table_RowException $e) {
  echo $e->getMessage();
}
&#63;>

Copy after login

Readers who are interested in more zend-related content can check out the special topics of this site: "Zend FrameWork Framework Introductory Tutorial", "php Excellent Development Framework Summary", "Yii Framework Introduction and Summary of Common Techniques", "ThinkPHP Introductory Tutorial" , "php object-oriented programming introductory tutorial", "php mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.

Articles you may be interested in:

  • Zend Framework Tutorial - Zend_Db_Table_Rowset Usage Example Analysis
  • Zend Framework Tutorial - Detailed Usage of Zend_Db_Table
  • Zend Framework Tutorial The Zend_Form component implements form submission and displays error prompts
  • Zend Framework development introductory classic tutorial
  • Zend Framework Smarty extension implementation method
  • Zend Framework routing mechanism code analysis
  • Zend Framework implements a guestbook with basic functions (with demo source code download)
  • Zend Framework implements the method of storing sessions in memcache
  • Detailed explanation of the usage of Zend Framework paging class
  • Zend Framework implements multiple file upload function example
  • Environment configuration for getting started with Zend Framework and the first Hello World example (with demo source code download)
  • Zend Framework tutorial link Database and method to perform add/delete query (with demo source code download)
  • Detailed explanation of the Zend_Db_Table table association instance in the Zend Framework tutorial

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1113741.htmlTechArticleZend Framework tutorial Zend_Db_Table_Row usage example analysis, zendframework2 example This article describes the Zend Framework tutorial Zend_Db_Table_Row usage example. Share it with everyone for your reference...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template