The following small series will bring you a PHP object-oriented transaction script mode (detailed description ). I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the following small series to bring you a PHP object-oriented transaction script mode (detailed description ). I think this is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian.
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
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 = Rray ($ 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 is a detailed explanation of the PHP object-oriented transaction script mode example. For more information, see other related articles in the first PHP community!