There are a lot of patterns in the design patterns in PHP, and here we are going to introduce you to a less commonly used data mapping model, and hopefully the article will help you all.
The data mapping model enables you to better organize your applications to interact with the database.
The data mapping mode reduces the binding density of the objects ' properties to the table fields where they are stored. The nature of a data mapping pattern is a class that maps or translates the properties of a class or the corresponding field of a method to a database, and vice versa.
The role of data mapping is to understand the information presented by both parties and to control access to information, such as based on the information stored in the datasheet
Rebuilds new domain objects or uses information from domain objects to update or delete related data in a datasheet.
There are several ways to implement storage for object-oriented code and mapping relationships between database tables and fields. One possible method is to store this mapping relationship in a data mapping class by hand coding.
An alternative approach is to use an array of PHP and encode it as the class itself. This class can also get data from an external source, such as an INI or an XML file.
The data object mapping pattern is the mapping of objects and data stores, and the operation of an object is mapped to operations on the data store.
Implement the Data object mapping pattern in code, implement an ORM class, and map complex SQL statements into the operation of object properties. Object Relational Mapping (relational mapping,orm)
HA_CL table
hacl.php
<?php
namespace Baobab;
Class hacl{public
$id;
public $haclname;
public $haclcode;
public $hacls;
protected $db;
function __construct ($id) {
$this->db = new \baobab\database\mysqli ();
$this->db->connect (' 127.0.0.1 ', ' root ', ' ', ' test ');
$res = $this->db->query ("select * from ha_cl where id = {$id}");
$data = $res->fetch_assoc ();
$this->id = $data [' id '];
$this->haclname = $data [' Ha_cl_name '];
$this->haclcode = $data [' Ha_cl_code '];
$this->hacls = $data [' hacls '];
}
function __destruct () {
$this->db->query ("Update ha_cl set
Ha_cl_code = ' {$this->haclcode} ')
ha_cl_name = ' {$this->haclname} ',
hacls = ' {$this->hacls} '
where ID = {$this->id}
limit 1 ");
}
}
factory.php
<?php
namespace Baobab;
Class factory{
static function Gethacl ($id) {
$key = ' user_ '. $id;
$user = \baobab\register::get ($key);//The table has different IDs that represent different objects
if (! $user) {
$user = new \baobab\hacl ($id);
\baobab\register::set ($key, $user);
}
return $user;
}
register.php
<?php
namespace Baobab;
Class register{
protected static $objects;
static function set ($alias, $object) {
self:: $objects [$alias] = $object;
}
static function _unset ($alias) {
unset (self:: $objects [$alias]);
}
static function Get ($name) {return
self:: $objects [$name];
}
}
index.php
Class page{
function Index () {
$hacl = Baobab\factory::gethacl;
$hacl->haclname = ' test name ';
$this->test ();
echo ' OK ';
}
function test () {
$hacl = Baobab\factory::gethacl;
$hacl->hacls = ' test content ';
}
}
$page = new page ();
Using Factory mode creates object Hacl multiple times, wasting resources, and if you pass objects as parameters, it can lead to additional cost of use, and if you use this object in many places it is easy to make mistakes, so use the registration Tree mode in Factory mode to solve the problem.
The above content to introduce the PHP design pattern of data Object mapping mode, I hope to help you!