自定义MVC框架之工具类-模型类

Posted ghostwu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义MVC框架之工具类-模型类相关的知识,希望对你有一定的参考价值。

截止目前已经改造了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框架之工具类-模型类的主要内容,如果未能解决你的问题,请参考以下文章

自定义MVC框架之工具类-分页类的封装

手写MVC框架

手写MVC框架

手写MVC框架

手写MVC框架

ExtJS 4(MVC)商店不使用我的自定义模型类