PHP in the design pattern has a lot of different patterns, here we introduce a less common data mapping mode, I hope the article can help you.
The data mapping pattern allows you to better organize your applications to interact with the database.
The data mapping pattern reduces the binding density between the object's properties and the table fields where they are stored. The essence of the data mapping pattern is a class that maps or translates the properties or methods of a class to the corresponding fields in the 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 information stored in a data table
Rebuilds new domain objects, or uses information from domain objects to update or delete related data in a data table.
There are several implementations of storage for object-oriented code and mapping between database tables and fields. One possible method is to store this mapping relationship in a data map class by hand coding.
Another alternative is to use an array of PHP and encode it as the class itself. This class can also fetch data from external sources, such as INI or XML files.
The data object mapping pattern is the mapping of objects and data stores, and operations on an object are mapped to operations on the data store.
Implement a data object mapping pattern in your code, implement an ORM class, and map complex SQL statements to the operations of object properties. Object Relational Mapping (objects relational Mapping,orm)
HA_CL table
hacl.php
<?phpnamespace baobab;class hacl{public $id;p ublic $haclname;p ublic $haclcode;p ublic $hacls;p rotected $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 Setha_cl_code = ' {$this->haclcode} ', Ha_cl_name = ' {$ This->haclname} ', Hacls = ' {$this->hacls} ' where ID = {$this->id}limit 1 ");}}
factory.php
<?phpnamespace baobab;class factory{static function Gethacl ($id) {$key = ' user_ '. $id; $user = \baobab\register::get ($ key);//the ID in the table represents a different object if (! $user) {$user = new \baobab\hacl ($id); \baobab\register::set ($key, $user);} return $user;}}
register.php
<?phpnamespace 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
Using Factory mode creates object Hacl multiple times, wasting resources, and if the object is passed as a parameter, with additional usage costs on one hand, and if many places use this object to be prone to errors, 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, we hope to help!