PHP design Pattern Notes and summaries (9) Data Object Mapping mode

Source: Internet
Author: User

"Data Object Mapping Mode"

is to map objects and data stores, and operations on an object are mapped to operations on the data store. For example, in the code of the new object, using the data Object mapping mode can be used to set some of the object's operations, such as setting some properties, will be automatically saved to the database, and the database in the table corresponding to a record.

"Code Implementation"

Implementing the data Object mapping pattern in your code, we will implement an ORM (object-relational-map-relational-Mapping) class that maps complex SQL statements into object-property operations. Combined with "Factory mode" and "Registration mode".

"Example 1"

Database test, User table structure:

CREATE TABLE ' user ' (  int(one) not NULL auto_increment,  ' name ' varchar (  + ) CHARACTER set UTF8 default null,  ' mobile ' varchar (one) CHARACTER SET UTF8 default NULL,  ' Regtime ' timestamp null DEFAULT null on UPDATE current_timestamp,  PRIMARY KEY (' id ')) ENGINE=innodb auto_incr ement=2 DEFAULT charset=latin1;

common\user.php:

<?phpnamespace Common;classuser{ Public $id;  Public $name;  Public $mobile;  Public $regtime; protected $db; //Construction Method    function__construct ($id) {        $this->db =NewDatabase\mysqli (); $conn=$this->db->connect (' 127.0.0.1 ', ' root ', ' ', ' test ')); $res=$this->db->query ("SELECT * from user where id = {$id} Limit 1 "); $data=$res-Fetch_assoc (); $this->id =$data[' ID ']; $this->name =$data[' Name ']; $this->mobile =$data[' Mobile ']; $this->regtime =$data[' Regtime ']; }        //destructor Method    function__destruct () {$this->db->query ("Update user set name = ' {$this->name} ', mobile = ' {$this->mobile} ', Regtime = ' {$this->regtime} ' where id = {$this->id} Limit 1 "); }}

common\databases\mysqli.php

<?phpnamespace common\database; Usecommon\idatabase;classMysqliImplementsidatabase{protected $conn; functionConnect$host,$user,$passwd,$dbname){        $conn=Mysqli_connect($host,$user,$passwd,$dbname); $this->conn =$conn; }        functionQuery$sql){        $res=Mysqli_query($this->conn,$sql); return $res; }    functionClose () {Mysqli_close($this-conn); }}

Entry file index.php

1<?PHP2Define'BASEDIR', __dir__);//defining a root directory constant3Include BASEDIR.'/common/loader.php';4Spl_autoload_register ('\\common\\loader::autoload');5Echo'<meta http-equiv= "Content-type" content= "Text/html;charset=utf8" >';6 7 /*8 * The operation of the object's properties completes the operation of the database.9  */Ten$user =NewCommon\user (1); One  A //reading Data -Var_dump ($user->id, $user->mobile, $user->name, $userregtime); exit (); -  the$user->mobile ='13800138000'; -$user->name ='Arshavin'; -$user->regtime = Date ("y-m-d h:i:s", time ());

Line 13 output (data from the original table):

 string   "   (Length=1  )  string   "   (Length=5  )  string   "   (Length=2  )  string   "   (Length=19  )

Note Line 13, access to the portal file, the database data is modified

PHP design Pattern Notes and summaries (9) Data Object Mapping mode

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.