Domestic initphp Frame Series-initphp framework to build high-availability Web application 05: Data Layer DAO usage

Source: Internet
Author: User
Tags pconnect php class

Initphp Framework is a lightweight PHP open source framework, framework documentation and: http://initphp.com

DAO Layer Description

The DAO layer is the data layer. In a nutshell, the DAO layer is mainly used to write SQL statements. May not have made the Java classmate will be unfamiliar to the DAO layer, even cannot accept.

But the introduction of the DAO layer has great benefits:

1. Split the business and data operations. For example, the module layer in the original MVC is split into service and DAO layers. Service is primarily used for business operations, while DAO is primarily responsible for data manipulation.

2. The original MVC pattern, the longer the project development, the longer the majority of SQL statements will be combined with the business, later maintenance will become extremely difficult, or even modify a table of fields need to find all the SQL statements to modify. After the introduction of DAO, the modification of SQL statements only needs to be modified directly in DAO. I have been involved in the Phpwind, because of historical reasons, has also done in order to modify a data table field and find all the files of the stamp.

3. After the introduction of the DAO layer, the operation of the sub-database is greatly facilitated. Because the SQL statements are all concentrated in one file, the direct modification is OK.

4. After the introduction of the DAO layer, it is convenient to migrate the storage medium, for example, the data table exists on MySQL, and now it can be easily migrated to MongoDB.

5. DAO layer Principle: General situation a DAO file corresponds to a database table, but it also allows multiple tables in a DAO (as far as the business is the same module).


DAO configuration

Here is the database configuration for our initphp framework:

/*********************************dao Database Configuration *****************************************//** * DAO configuration parameter * 1. You can configure the path of DAO and the suffix name of the file (class name) * 2. In general you do not need to change this configuration */$InitPHP _conf[' dao ' [' dao_postfix '] = ' dao '; suffix $initphp_conf[' dao ' [' path '] = ' library/dao/'; Suffix/** * Database configuration * 1. Configure * 2 According to the database condition of the project. Support single database server, read/write separation, random distribution way * 3. MySQL mysqli (both) * 4 can be selected according to $initphp_conf[' db ' [' Default '] [' db_type ']. Supports multi-Library configuration $InitPHP _conf[' db ' [' Default '] * 5. See document in detail * * $InitPHP _conf[' db ' [' driver '] = ' mysqli '; Select a different DB engine, general default mysqli, or Mysqls//default database configuration is generally used $this->init_db (' Default '), or $this->init_db ()- > for the default model $initphp_conf[' DB ' [' Default '] [' db_type '] = 0; 0-Single Server, 1-read-write separation, 2-random $initphp_conf[' db ' [' Default '][0][' host '] = ' 127.0.0.1 '; Host $initphp_conf[' db ' [' Default '][0][' username '] = ' root '; Database user name $initphp_conf[' db ' [' Default '][0][' password '] = ' root '; Database Password $initphp_conf[' db ' [' Default '][0][' database '] = ' test '; //Database $initphp_conf[' db ' [' Default '][0][' charset '] = ' UTF8 '; Database encoding $InitPHP _conf[' db ' [' Default '][0][' pconnect '] = 0;                      Whether the persistent link//test database configuration uses: $this->init_db (' Test ')--read/write separation, random selection (with two databases) $InitPHP _conf[' db ' [' Test '] [' Db_type '] = 2; 0-Single Server, 1-read-write separation, 2-random $initphp_conf[' db ' [' Test '][0][' host '] = ' 127.0.0.1 '; Host $initphp_conf[' db ' [' Test '][0][' username '] = ' root '; Database user name $initphp_conf[' db ' [' Test '][0][' password '] = '; Database Password $initphp_conf[' db ' [' Test '][0][' database '] = ' T1 '; Database $initphp_conf[' db ' [' Test '][0][' charset '] = ' UTF8 '; Database encoding $InitPHP _conf[' db ' [' Test '][0][' pconnect '] = 0; Whether the persistent Link $initphp_conf[' db ' [' Test '][1][' host '] = ' 127.0.0.1 '; Host $initphp_conf[' db ' [' Test '][1][' username '] = ' root '; Database user name $initphp_conf[' db ' [' Test '][1][' password '] = '; Database Password $INITPhp_conf[' db ' [' Test '][1][' database '] = ' T1 '; Database $initphp_conf[' db ' [' Test '][1][' charset '] = ' UTF8 '; Database encoding $InitPHP _conf[' db ' [' Test '][1][' pconnect '] = 0; Whether persistent links

The initphp supports multiple database connections and also supports read and write separations.

Our side mainly only the operation of a single table.


Basic use 1. Create a data table

We create a very simple user table in the test library:

Table structure:

CREATE TABLE ' user ' (  ' id ' int ') NOT NULL auto_increment,  ' username ' varchar (255) is not NULL,  ' password ' Varc Har (255) not NULL,  PRIMARY KEY  (' id ')) engine=myisam  DEFAULT Charset=utf8  ;


2. Create a DAO

Configuration file, we configured the relevant configuration of DAO:

$InitPHP _conf[' dao ' [' dao_postfix ']  = ' DAO ';//suffix $initphp_conf[' dao ' [' path ']  = ' library/dao/';//suffix

So we created the DAO file put in the library/dao/folder, suffix name dao


Userdao need to inherit the base class of DAO

<?php class Userdao extends Dao {public $table _name = ' user ';p rivate $fields = "Username,password";/** * New user * @param $user */public function AddUser ($user) {$user = $this->init_db ()->build_key ($user, $this->fields); return $ this->init_db ()->insert ($user, $this->table_name);} /** * Get a data by ID * @param unknown_type $id */public function GetUser ($id) {return $this->init_db ()->get_one ($id, $th is->table_name);}}

3. Calling DAO in service

We have a userservice:


UserService need to inherit base class service

<?php/** * Demo Service Test * @author Zhuli */class UserService extends Service {/** * @var userdao */private $userDao;p u Blic function __construct () {parent::__construct (); $this->userdao = Initphp::getdao ("user");} /** * Gets a user * @param int $id */public function GetUser ($id) {if ($id < 1) {return array ();} return $this->userdao->getuser ($id);} /** * Create a user */public function CreateUser ($user) {return $this->userdao->adduser ($user);}

4. Call in Controller

Call service in controller to add and retrieve a user information

Indexcontroller need to inherit base class controller

<?php/** * initphp Open Source Framework-DEM * @author zhuli */class Indexcontroller extends Controller {public $initphp _list = Array () ; Action Whitelist//Use Zend Editor, add @var comment to the variable above, the editor can prompt UserService method/** * @var userservice */private $userService;p ublic function __construct () {parent::__construct (); $this->userservice = Initphp::getservice ("user");} Public Function Run () {$user = Array ("username" = "Zhuli", "password" = "123456"); $id = $this->userservice-> CreateUser ($user); echo "UserId:". $id. "<br/>"; $userInfo = $this->userservice->getuser ($id);p rint_r ($userInfo);}}

5. Results in the browser


These are the simplest ways to use the DAO layer. For complex use see intphp official website document: http://initphp.com/4_2.htm


Domestic initphp Frame Series-initphp framework to build high-availability Web application 05: Data Layer DAO usage

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.