1. 程式人生 > >自定義MVC框架之工具類-模型類

自定義MVC框架之工具類-模型類

else pri 數據庫連接 執行 field date http one www.

截止目前已經改造了5個類:

ubuntu:通過封裝驗證碼類庫一步步安裝php的gd擴展

自定義MVC框架之工具類-分頁類的封裝

自定義MVC框架之工具類-文件上傳類

自定義MVC框架之工具類-圖像處理類

這個模型類支持以下功能:

>連貫操作,js叫鏈式操作,連貫操作的函數可以打亂順序,最後一個函數必須是執行語句的那個函數,如select, delete, update, add等

如 $db->table( ‘user‘ )->where( ‘id=1‘ )->select()

等等

->支持擴展各種子類模型,命名如: UserModel, 也可以用其他方式的,跟表名稱不同的Model命名,如MyUserModel等

技術分享圖片
  1 <?php
  2 
  3 class Model {
  4     protected $dbHost; //主機
  5     protected $dbUser; //用戶名
  6     protected $dbPwd; //密碼
  7     protected $dbName; //數據庫名
  8 
  9     protected $prefix; //表前綴
 10     protected $tbName; //表名稱
 11     protected $charset; //字符集
 12     protected $link; //數據庫連接資源
13 protected $sql; //拼接的sql語句 14 protected $options; //sql查詢條件 15 16 17 public function __construct( $config ){ 18 $this->dbHost = $config[db_host]; 19 $this->dbUser = $config[db_user]; 20 $this->dbPwd = $config[db_pwd]; 21 $this
->dbName = $config[db_name]; 22 $this->prefix = $config[prefix]; 23 $this->charset = $config[charset]; 24 25 $this->link = $this->connect(); 26 27 $this->tbName = $this->getTableName(); 28 29 $this->initOptions(); 30 } 31 32 protected function initOptions(){ 33 $sqlKey = [ where, group, having, limit, order, field, table ]; 34 foreach ( $sqlKey as $key => $value ) { 35 $this->options[$value] = ‘‘; 36 if( $value == table ) { 37 $this->options[$value] = $this->tbName; 38 } 39 } 40 } 41 42 public function table( $tbName ) { 43 if( !empty( $tbName ) ) { 44 $this->options[table] = $tbName; 45 } 46 return $this; 47 } 48 49 public function limit( $limit ){ 50 if( !empty( $limit ) ) { 51 if( is_string( $limit ) ) { 52 $this->options[limit] = limit . $limit; 53 }else if ( is_array( $limit ) ) { 54 $this->options[limit] = limit . join( ,, $limit ); 55 } 56 } 57 return $this; 58 } 59 60 public function order( $order ){ 61 if( !empty( $order ) ) { 62 $this->options[order] = order by . $order; 63 } 64 return $this; 65 } 66 67 public function having( $having ){ 68 if( !empty( $group ) ) { 69 $this->options[having] = having . $having; 70 } 71 return $this; 72 } 73 74 public function group( $group ){ 75 if( !empty( $group ) ) { 76 $this->options[group] = group by . $group; 77 } 78 return $this; 79 } 80 81 public function where( $where ){ 82 if( !empty( $where ) ) { 83 $this->options[where] = where . $where; 84 } 85 return $this; 86 } 87 88 public function field( $fields ){ 89 if( !empty( $fields ) ) { 90 if( is_string( $fields ) ) { 91 $this->options[field] = $fields; 92 }else if( is_array( $fields ) ){ 93 $this->options[field] = join( ,, $fields ); 94 } 95 } 96 return $this; 97 } 98 99 public function getTableName(){ 100 if( !empty( $this->tbName ) ) { 101 return $this->prefix . $this->tbName; 102 } 103 $className = get_class( $this ); 104 //UserModel GoodsModel, 獲取Model前面的字符串(就是表名稱) 105 $tbName = strtolower( substr( $className, 0, -5 ) ); 106 return $this->prefix . $this->tbName; 107 } 108 109 protected function connect(){ 110 $link = mysql_connect( $this->dbHost, $this->dbUser, $this->dbPwd ); 111 if( !$link ){ 112 die( "mysql數據庫連接失敗:" . mysql_error() ); 113 } 114 mysql_select_db( $this->dbName, $link ); 115 mysql_query( $this->charset ); 116 return $link; 117 } 118 119 public function select(){ 120 $sql = SELECT %FIELD% FROM %TABLE% %WHERE% %GROUP% %HAVING% %ORDER% %LIMIT%; 121 $sql = str_replace( 122 [%FIELD%, %TABLE%, %WHERE%, %GROUP%, %HAVING%, %ORDER%, %LIMIT%], 123 [ $this->options[field], $this->options[table], $this->options[where], $this->options[group], $this->options[having], $this->options[order], $this->options[limit] ], 124 $sql 125 ); 126 $this->sql = $sql; 127 return $this->query( $sql ); 128 } 129 130 public function __get( $key ){ 131 if( $key == sql ) { 132 return $this->sql; 133 }else if( $key == prefix ) { 134 return $this->prefix; 135 } 136 return false; 137 } 138 139 public function query( $sql ){ 140 //執行語句之前,清空原來的options保存的sql語句臨時拼接數據 141 $this->initOptions(); 142 $res = mysql_query( $sql ); 143 $data = array(); 144 if( $res && mysql_num_rows( $res ) ) { 145 while( $row = mysql_fetch_assoc( $res ) ){ 146 $data[] = $row; 147 } 148 } 149 return $data; 150 } 151 152 public function add( $data ){ 153 $data = $this->parse( $data ); 154 $keys = array_keys( $data ); 155 $values = array_values( $data ); 156 $sql = INSERT INTO %TABLE%(%FIELD%) values(%VALUES%); 157 $sql = str_replace( 158 [ %TABLE%, %FIELD%, %VALUES% ], 159 [ $this->options[table], join( ,, $keys ), join( ,, $values ) ], 160 $sql 161 ); 162 $this->sql = $sql; 163 return $this->exec( $sql, true ); 164 } 165 166 public function delete(){ 167 $sql = DELETE FROM %TABLE% %WHERE%; 168 $sql = str_replace( 169 [ %TABLE%, %WHERE% ], 170 [ $this->options[table], $this->options[where] ], 171 $sql 172 ); 173 $this->sql = $sql; 174 return $this->exec( $sql ); 175 } 176 177 public function exec( $sql, $isInsert = false ){ 178 $this->initOptions(); 179 $res = mysql_query( $sql ); 180 if( $res !== false ) { 181 if( $isInsert ) { 182 return mysql_insert_id(); 183 }else { 184 return mysql_affected_rows(); 185 } 186 } 187 return false; 188 } 189 190 public function parse( $data ) { 191 $res = []; 192 foreach( $data as $k => $v ){ 193 if( is_string( $v ) ) { 194 $res[$k] = " . $v . "; 195 } 196 } 197 return $res; 198 } 199 200 public function update( $data ) { 201 $data = $this->parse( $data ); 202 $fieldValue = $this->format( $data ); 203 $sql = UPDATE %TABLE% SET %FIELD% %WHERE%; 204 $sql = str_replace( 205 [ %TABLE%, %FIELD%, %WHERE% ], 206 [ $this->options[table], $fieldValue, $this->options[where] ], 207 $sql 208 ); 209 $this->sql = $sql; 210 return $this->exec( $sql ); 211 } 212 213 //update ghostwu_user set field = value, where .... 214 protected function format( $data ){ 215 $res = []; 216 foreach( $data as $k => $v ) { 217 $res[] = $k . = . $v; 218 } 219 return join( ,, $res ); 220 } 221 222 public function __destruct(){ 223 mysql_close( $this->link ); 224 } 225 226 public function __call( $funcName, $args ) { 227 $str = substr( $funcName, 0, 5 ); 228 $field = substr( $funcName, 5 ); 229 if( $str == getBy ) { 230 echo $args[0]; 231 return $this->where( $field . =" . $args[0] . " )->select(); 232 } 233 return false; 234 } 235 } 236 $config = [ 237 db_host => localhost, 238 db_user => root, 239 db_pwd => ‘‘, 240 prefix => ghostwu_, 241 db_name => blog, 242 charset => utf8, 243 ]; 244 $db = new Model( $config ); 245 print_r( $db->field( * )->table( $db->prefix . users )->getByName( zhangsan ) ); 246 echo $db->sql . PHP_EOL; 247 //$db->table( $db->prefix . ‘users‘ )->where( ‘id=1‘ )->update( [ ‘name‘ => ‘david‘, ‘email‘ => ‘[email protected]‘ ] ); 248 //echo $db->sql . PHP_EOL; 249 //$db->table( $db->prefix . ‘users‘ )->where( ‘id in( 4, 5, 6, 7 )‘ )->delete(); 250 //echo $db->sql . PHP_EOL; 251 //$db->table( $db->prefix . ‘users‘ )->add( [‘name‘ => ‘zhangsan‘, ‘email‘ => ‘[email protected]‘] ); 252 /* 253 $list = $db->table( $db->prefix . ‘users‘ )->field( [ ‘name‘, ‘email‘ ] )->where( ‘id >= 1‘ )->order( ‘id desc‘ )->limit( [0, 5] )->select(); 254 echo $db->sql . PHP_EOL; 255 print_r( $list ); 256 */ 257 ?>
View Code

自定義MVC框架之工具類-模型類