There are a lot of patterns in the design pattern in PHP, here we will introduce you to a infrequently used data mapping mode, interested friends to see it
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 = $dat a[' 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
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 (); $page->index ();
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 in many places where it is prone to errors, use the registration tree pattern in Factory mode to solve the problem.