Summary domain model + data instance-php Tutorial

Source: Internet
Author: User
Tags rewind
* Here I want to explain that the content of related articles in my lazy blog is more about & lt; in-depth PHP object-oriented, pattern and practice & gt; the code in this book is organized and annotated to facilitate future review and reference. if you are interested in the relevant content, read the original article first. The content here can only be a supplement and reference for learning. Thank you! Because the sample code of the domain model + data er in the original book is consistent, it is organized here. I would like to briefly introduce my views. from the perspective of database operations, domain models mainly involve operations.
/* Here I want to explain that the content of related articles in my lazy blog is more
 <深入php面向对象、模式与实践>
  
The code in this book is organized and annotated to facilitate future review and reference. if you are interested in the relevant content, read the original article first. The content here can only be a supplement and reference for learning. Thank you! Because the sample code of the domain model + data er in the original book is consistent, it is organized here. In my opinion, from the perspective of database operations, the domain model mainly operates on a single record in a data table, while the data er operates on the data of the entire data table. According to the original article, the data Tor is a class responsible for ing database data to objects, and the domain model symbolizes various participants in projects in the real world, it is usually a record in data. The code and annotations are as follows: the three data table structures related to the domain model are venue, space, and 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 (Only one Venue class is created here 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 has no specific implementation, it should be used to obtain the subordinate object of an object, such as the space related to the venue (location) object // the specific code implementation should query the relevant data from the database and call the Collection class, the following shows that this class will have an understanding of // and the implementation of this method should be placed in the subclass in the static function getCollection ($ type) {return array ();} function collection () {return self: getC Ollection (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 '); // here I guess parent :__ construct ($ id);} function setSpaces (SpaceCollection $ spaces) {$ this-> spaces = $ spaces ;} function addSpace (Space $ space) {$ this-> spaces-> add ($ space); $ spac E-> setVenue ($ this);} function setName ($ name_s) {$ this-> name = $ name_s; $ this-> markDirty ();} function getName () {return $ this-> name ;}// data er (as explained in the original article, data ER is a class responsible for ing database data to objects) namespace woo \ mapper; abstract class Mapper {// abstract base class abstract static $ PDO; // operate the 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 $ obj = $ this-> doCreateObject ($ array) in the above domain model; // implement return $ obj in the subclass ;} function find ($ id) {// Obtain a piece of data from the database by ID and create it as an object $ this-> selectStm T ()-> 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 to the database $ this-> doInsert ($ obj );} // abstract function update (\ woo \ domain \ DomainObject $ objet), and protected abstract function doCreateObject (array $ array ); protected abstract function selectStmt (); protected abstract function doInsert (\ woo \ domain \ Doma InObject $ 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 the return new SpaceCollection ($ raw, $ this) object ); // the base class of this class is below} protected function doCreateObject (array $ array) {// create an object $ obj = new \ woo \ domain \ Venue ($ array ['id']); $ obj-> setname ($ array ['name']); return $ obj;} protected function doInsert (\ woo \ domain \ DomainObject $ object) {// Insert the object into the database print 'inserting '; debug_print_backtrace (); $ values = array ($ obje Ct-> getName (); $ this-> insertStmt-> execute ($ values); $ id = self: $ PDO-> lastInsertId (); $ object-> setId ($ id);} function update (\ woo \ domain \ DomainObject $ object) {// modify the database data print "updation \ n "; $ values = array ($ object-> getName (), $ object-> getId (), $ object-> getId ()); $ this-> updateStmt-> execute ($ values);} function selectStmt () {// return an SQL statement object return $ this-> selectStmt ;}} /* Iterator interface definition method: rewind () points to the beginning of the list curren T () returns the element key () at the current pointer and returns the current key (for example, pointer) next () valid (). the class below processes records of multiple rows, pass the raw data and er retrieved from the database, and then use the er to create the data as an object */abstract class Collection implements \ Iterator {protected $ mapper; // data protected $ total = 0; // total number of set elements protected $ raw = array (); // raw data private $ result; private $ pointer = 0; // pointer private $ objects = array (); // 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) {// directly add the object $ class =$ this-> targetClass (); if (! ($ Object instanceof $ class) {throw new Exception ("This is a {$ class} collection") ;}$ this-> policyaccess (); $ this-> objects [$ this-> total] = $ object; $ this-> total ++;} abstract function targetClass (); // implement the protected function policyaccess () {// Unknown when inserting an object in the subclass} private function getRow ($ num) {// Obtain a single piece of data in the set. here, the data is created as an object $ this-> policyaccess () through the data ER (); 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 an object to the database $ mapper-> insert ($ venue ); // read the inserted object 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); // end
 

The above is a summary of the details of the domain model + data er instance. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.