The following small series for everyone to bring a PHP object-oriented Transaction script mode (detailed). Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
As shown below:
/* Transaction script mode: Similar to the model layer in thinkphp, or the class that operates the database. Personally feel that the use of practice is quite simple and convenient, that is, the SQL statement written dead, flexibility is not enough. The sample code is as follows: */namespace woo\process;abstract class base{static $DB; PDO object Static $stmts = Array (); SQL statement handle function construct () {$DSN = \woo\base\applicationregistry::getdsn (); if (Is_null ($DSN)) {throw new \woo\base\appexception ("No DSN"); } self:: $DB = new PDO ($DSN); Self:: $DB->setattribute (\pdo::attr_errmode,\pdo::errmode_exception); } function Preparestatement ($stmt _s) {//cache SQL statement handle if (Isset (self:: $stmts ($stmt _s)) {Return to self:: $stmts [$stmt _s ]; } $stmt _handle = self:: $DB->prepare ($stmt _s); Self:: $stmts [$stmt _s] = $stmt _handle; return $stmt _handle; } protected function Dostatement ($stmt _s, $values _a) {//Execute SQL and get a statement resource $sth = $this->preparestatement ($stmt _s); $sth->closecursor (); $db _result = $sth->execute ($values _a); return $sth; }}//This class is to write some data to the database, relatively simple not much to say the class Venuemanager extends base{static $add _venue = INSERT into venue (name) values (?); static $add _space = "INSERT into space (name,venue) VALUES (?,?)"; static $check _slot = "Select Id,name from event WHERE space =?" and (start+duration); and start <? "; static $add _event = "INSERT into event (name,space,start,duration) VALUES (?,?,?,?)"; function Addvenue ($name, $space _array) {$ret = array (); $ret [' venue '] = array ($name); $this->dostatement (self:: $add _venue, $ret [' venue ']); $v _id = self:: $DB->lastinsertid (); $ret [' spaces '] = array (); foreach ($space _array as $space _name) {$values = array ($space _name, $v _id); $this->dostatement (self:: $add _space, $values); $s _id = self:: $DB->lastinsertid (); Array_unshift ($values, $s _id); $ret [' spaces '] [] = $values; } return $ret; } function Bookevent ($space _id, $name, $time, $duration) {$values = array ($space _id, $time, ($time + $duration)); $stmt = $this->dostatement (self:: $check _slot, $values, false); if ($result = $stmt->fEtch ()) {throw new \woo\base\appexception ("Double booked! Try again "); } $this->dostatement (self:: $add _event,array ($name, $space _id, $time, $duration)); }}//client, it is relatively easy to use $venue = new Venuemanager (), $venue->addvenue (' Test ', Array (' test1 ', ' test2 ', ' test3 ');
Above this PHP object-oriented Transaction script mode (detailed) is the small part to share all the content of everyone, I hope to give you a reference, but also hope that we support the script home.