Several previous design patterns have greatly improved the readability and maintainability of the PHP code. However, there are more important requirements and challenges in Web applications: Database applications. But the previous design pattern was not involved in this. Today, the data mapping model is a better way to organize your applications to interact with the database.
Bloggers have spent a bit of time on these two days, and this pattern has a little bit of understanding. Bold in this shortcoming, according to their own understanding, write a little something to share with you, learn from each other.
Of course, when it comes to data mapping patterns, we have to mention object-relational mapping (relational Mapping, or ORM), which is used to transform data between different types of systems in object-oriented programming languages. General ORM Framework to deal with a simple application system can meet the basic requirements, can greatly reduce the difficulty of development, improve development efficiency, but it in the SQL optimization, it is certainly a lot worse than the pure SQL language, the complex association, SQL embedded Expression processing is not very ideal.
For the TP framework currently used by bloggers, its core file Model.class.php is to implement ORM and Activerecords mode, and all models in the project inherit this model class.
Well, or not ashamed to say this nonsense, and their own reference to compile an example, to share with each other, exchange.
First we need a database middle-tier implementation class that uses PDO for database access. Of course, this class is not the focus of today, I also copied from the Internet, you can directly ignore.
Create a Db class file Db.class.php
<?php/* * Database middle-tier implementation class */class db { public static $DB = null; private $_dbh = null; public static function getinstance () { if ( self:: $db == null ) { self:: $db = new self (Backend_dbhost ,backend_dbuser ,backend_dbpw ,backend_dbname); } return self:: $db; } private function __construct ( $host , $user , $pass $dbname ) { try { $this->_dbh = new pdo (' mysql:dbname= '. $dbname. ') Host= '. $host, $user, $pass); $this- >_dbh->query (' set names '. backend_dbcharset); $this->_dbh->setattribute (pdo::mysql_attr_use_buffered_query, true); $this->_dbh-> SetAttribute (pdo::attr_errmode, true); } catch (pdoexception $e) { throw new exception (' can not connect db '); }  } public function getone ($ SQL) { &NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; try { $rs = $this->_dbh->query ($sql); $result = $rs->fetch (PDO::FETCH_ASSOC); if (!empty ($result)) { return $result; } } catch (pdoexception $e) { throw new exception ($this->_dbh->errorinfo ()); } return false; } public function getall ($sql) { try { $rs = $this->_dbh->query ($sql); $result = $rs->fetchall (PDO::FETCH_ASSOC); if (!empty ($result)) { return $result; } } catch (pdoexception $e) { throw new exception ($this->_dbh->errorinfo ()); } return false; } public function exec ($sql) { try { $exec = $this->_dbh->exec ($sql); catch (pdoexception $e) { throw new exception ($this->_dbh->errorinfo ());
} return $exec; &NBSP;&NBSP;&NBSP;&NBSP} public function getlastid () { return $this->_dbh->lastinsertid (); } } ?>
Data Map Class Table.class.php
<?php/** * Data mapping class * part code source TP framework * using related Magic method then the mapped table modifies the field without modifying the property value */ class table{ // data information protected $data
= array ();
// data protected $db = null;
// table information protected $tableName = '; public function __construct () {
$this->db = db::getinstance (); &NBSP;&NBSP;&NBSP;&NBSP} /** * setting values for data Objects */ public function __set ($name, $value) { // set data Object Properties $
this->data[$name] = $value; &NBSP;&NBSP;&NBSP;&NBSP} /** * Get the value of the data object */ public function __get ($name) {
return isset ($this->data[$name]) $this->data[$name]:null; &NBSP;&NBSP;&NBSP;&NBSP} /* * add * Modify, delete also and add similar, do not list the */ public function add () { $data = $this->
Data foreach ($data as $k => $v) {
$FIELDARR [] = $k;
$valueArr [] = "'" $v. "'"; &nbSP;}
$fields = implode (', ', $fieldArr);
$values = implode (', ', $valueArr); $sql = ' insert into '. $this->tablename. ' ('. $fields. ')
VALUES ('. $values. ') ';
$result = $this->db->exec ($sql); if ($result) {
return $this->db->getlastid (); } else {
return false; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}  }}?>
Table-corresponding class file UserTable.class.php
<?php/** * Data map to tables * generally based on the structure of the table automatically generated by tools, such as the YII framework. */class usertable extends table { protected $
tablename = ' user '; ?> Usage index.php <?php/** * Database configuration file */ define (' Backend_ Dbhost ', ' localhost '); define (' Backend_dbuser ', ' root '); define (' BACKEND_DBPW ', ');
Define (' Backend_dbname ', ' test '); define (' Backend_dbcharset ', ' utf-8 '); /* * Here you can instantiate and manage an instantiated object by using the Factory mode and registration mode described earlier in this article * tp the D method in the framework is doing this part of the work */$UserTable =
new usertable ();
$UserTable->username = ' Anrai ';
$UserTable->mobile = ' 123456789 ';
$UserTable->email = ' huanglei.web@gmail.com ';
echo $UserTable->add ();
/* Datasheet SQL create table ' user ' ( ' uid ' int (one) not null auto_increment, ' username ' vaRchar ( not null), ' mobile ' varchar (one) NOT NULL DEFAULT ' 0 ', ' Email ' varchar NOT NULL DEFAULT ' 0 ', PRIMARY KEY (' uid '), key ' username ' (' username ')) engine=innodb default charset=latin1 * *?>