/* 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 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 ');