PHP Object-oriented transaction scripting mode

Source: Internet
Author: User
Tags dsn
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.

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.