In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
PHP object-oriented domain model + data mapper example introduction, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.
Because the sample code of the domain model + data mapper in the original book is coherent, it is sorted together here.
To briefly introduce my point of view, from the point of view of database operation, the domain model mainly manipulates a single record in the data table, while the data mapper operates the data of the entire data table.
According to the original interpretation, the data mapper is a class responsible for mapping database data to objects, while the domain model symbolizes the participants in the real-world project, which is usually represented as a record in the data.
Needless to say, the code and notes are as follows:
The three data table structures related to the domain model are venue (place), space (space) and event (event).
Create table 'venue' (' id' int (11) not null auto_increment, 'name' text, primary key (' id')) create table 'space' (' id' int (11) not null auto_increment, 'venue' int (11) default null,' name' text, primary key ('id')) create table' event' ('id' int (11) not null auto_increment,' space' int (11) default null, 'start' mediumtext 'duration' int (11) default null,' name' text, primary key ('id')) / / Domain model (here only one Venue class is built for understanding) namespace woo\ domain Abstract class DomainObject {/ / Abstract base class private $id; function _ _ construct ($id=null) {$this- > id= $id;} function getId () {return $this- > id } / / the original book does not have a specific implementation, but should be used to obtain the dependent objects of the object, such as venue-related space (space) objects / / the specific code implementation should query the relevant data from the database and call the Collection class When you see this class below, you will have an understanding of / / and the implementation of this method should be placed in a subclass for static function getCollection ($type) {return array () } function collection () {return self::getCollection (get_class ($this));}} class Venue extends DomainObject {private $name; private $spaces; function _ construct ($id = null,$name=null) {$this- > name= $name; $this- > spaces = self::getCollection ('\ woo\\ domain\ space'); / / this should prove my above guess parent::__construct ($id) } function setSpaces (SpaceCollection $spaces) {$this- > spaces = $spaces;} function addSpace (Space $space) {$this- > spaces- > add ($space); $space- > setVenue ($this);} function setName ($name_s) {$this- > name = $name_s; $this- > markDirty ();} function getName () {return $this- > name }} / / data mapper (as explained originally, data mapper is a class responsible for mapping database data to objects) namespace woo\ mapper;abstract class Mapper {/ / abstract base class abstract static $PDO; / / pdo object function _ _ construct () {if (! isset (self::$PDO) {$dsn =\ woo\ base\ ApplicationRegistry::getDSN () If (is_null ($dsn)) {throw new\ woo\ base\ AppException ("no dns");} self::$PDO = new\ PDO ($dsn); self::$PDO- > setAttribute (\ PDO::ATTR_ERRMODE,\ PDO::ERRMODE_EXCEPTION);}} function createObject ($array) {/ / create the array as the object in the domain model $obj = $this- > doCreateObject ($array) / / implement return $obj;} function find ($id) {/ / get a piece of data from the database through ID and create it as an object $this- > selectStmt ()-> execute (array ($id)); $array= $this- > selectStmt ()-> fetch (); $this- > selectStmt ()-> closeCursor (); if (! is_array ($array)) {return null } if (! isset ($array ['id'])) {return null;} $object = $this- > createObject ($array); return $object;} function insert (\ woo\ domain\ DomainObject $obj) {/ / insert object data into the database $this- > doInsert ($obj);} / / abstract methods abstract function update (\ woo\ domain\ DomainObject $objet) to be implemented in subclasses; protected abstract function doCreateObject (array $array) Protected abstract function selectStmt (); protected abstract function doInsert (\ woo\ domain\ DomainObject $object);} / / only one VenueMapper class is created here to understand class VenueMapper extends Mapper {function _ construct () {parent::__construct (); / / various sql statement objects $this- > selectStmt = self::$PDO- > prepare ("select * from venue where id=?"); $this- > updateStmt = self::$PDO- > prepare ("update venue set name=?,id=?") Where id=? "); $this- > insertStmt = self::$PDO- > prepare (" insert into venue (name) values ");} protected function getCollection (array $raw) {/ / convert the Space array to an object return new SpaceCollection ($raw,$this); / / the base class of this class is below} protected function doCreateObject (array $array) {/ / create the object $obj = new\ woo\ domain\ Venue ($array ['id']) $obj- > setname ($array ['name']); return $obj;} protected function doInsert (\ woo\ domain\ DomainObject $object) {/ / insert objects into database print' inserting'; debug_print_backtrace (); $values = array ($object- > getName ()); $this- > insertStmt- > execute ($values); $id = self::$PDO- > lastInsertId (); $object- > setId ($id) } function update (\ woo\ domain\ DomainObject $object) {/ / modify database data print "updation\ n"; $values = array ($object- > getName (), $object- > getId (), $object- > getId ()); $this- > updateStmt- > execute ($values);} function selectStmt () {/ / returns a sql statement object return $this- > selectStmt;}
The methods defined by the Iterator interface:
Rewind () points to the beginning of the list
Current () returns the element at the current pointer
Key () returns the current key (for example, pointer finger)
Next ()
Valid ()
The following class handles multiple rows of records, passing in the raw data extracted from the database and the mapper, and then creating it as an object when the data is obtained through the data mapper
Abstract class Collection implements\ Iterator {protected $mapper; / / data mapper protected $total = 0; / / Total number of collection elements protected $raw = array (); / / Raw data private $result; private $pointer = 0; / / pointer private $objects = array () / object collection function _ _ construct (array $raw = null,Mapper $mapper= null) {if (! is_null ($raw) & &! is_null ($mapper)) {$this- > raw = $raw; $this- > total = count ($raw);} $this- > mapper= $mapper;} function add (\ woo\ domain\ DmainObject $object) {/ / here is the direct addition of objects $class = $this- > targetClass () If (! ($object instanceof $class)) {throw new Exception ("This is a {$class} collection");} $this- > notifyAccess (); $this- > objects [$this- > total] = $object; $this- > total +;} abstract function targetClass () / / the protected function notifyAccess () {/ / unknown} private function getRow ($num) {/ / which is implemented in the subclass to check the type when inserting the object {/ / gets a single piece of data in the collection, which is where the data is created as an object $this- > notifyAccess (); if ($num > = $this- > total | | $num
< 0){ return null; } if(isset($this->Objects [$num]) {return $this- > objects [$num];} if (isset ($this- > raw [$num]) {$this- > objects [$num] = $this- > mapper- > createObject ($this- > raw [$num]); return $this- > objects [$num];} public function rewind () {/ / reset pointer $this- > pointer = 0 } public function current () {/ / get the current pointer object return $this- > getRow ($this- > pointer);} public function key () {/ / get the current pointer return $this- > pointer;} public function next () {/ / get the current pointer object and move the pointer down $row = $this- > getRow ($this- > pointer); if ($row) {$this- > pointer + +} return $row } public function valid () {/ / verify return (! is_null ($this- > current ());}} / subclass class VenueColletion extends Collection implements\ woo\ domain\ VenueCollection {function targetClass () {return "\ woo\ domain\ Venue";} / / client $mapper = new\ woo\ mapper\ VenueMapper (); $venue = $mapper- > find (12); print_r ($venue); $venue = new\ woo\ domain\ Venue (); $venue- > setName ("the likey lounge-yy") / insert the object into the database $mapper- > insert ($venue); / / read the object just inserted from the database $venue = $mapper- > find ($venue- > getId ()); print_r ($venue); / / modify the object $venue- > setName ("the bibble beer likey lounge-yy"); / / call update to update the record $mapper- > update ($venue); / / read the object data again $venue = $mapper- > find ($venue- > getId ()); print_r ($venue) / / is it helpful for you to finish reading the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.