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.