PHP object-oriented transaction Script Mode (detailed description), php object-oriented

Source: Internet
Author: User

PHP object-oriented transaction Script Mode (detailed description), php object-oriented

As follows:

/* Transaction Script Mode: similar to the model layer in thinkphp, or the database operation class. I personally think it is quite simple and convenient to use in practice, that is, if SQL statements are written to death, the flexibility is not enough. The sample code is as follows: */namespace woo \ process; abstract class Base {static $ DB; // pdo object static $ cmdts = 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 (s) Elf ::$ effects ($ stmt_s) {return self ::$ effects [$ stmt_s] ;}$ stmt_handle = self ::$ DB-> prepare ($ stmt_s ); self: $ effects [$ stmt_s] = $ stmt_handle; return $ stmt_handle;} protected function doStatement ($ stmt_s, $ values_a) {// execute the SQL statement and obtain a statement resource $ things = $ this-> prepareStatement ($ stmt_s); $ things-> closeCursor (); $ db_result = $……-> execute ($ values_a); return $…… }}// this class is used to write some data to the database, class VenueManager extends is quite simple. 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 ['space'] = 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, $ venue = new VenueManager (); $ venue-> addVenue ('test', array ('test1', 'test2', 'test3 '));

The above PHP object-oriented transaction Script Mode (detailed description) is all the content shared by the editor. I hope to give you a reference and support for the customer's house.

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.