一 Model 层

  在php框架中model 有没有必要存在,很有必要. 是否需要每一个表单独写一个model ?? 很多只有几个字段的表,她们除了增删改查之外没有任何独特的业务逻辑,而增删改查是每个数据都需要的基本逻辑. 完全可以抽象到一个父类.

二 model 设计

  model 层封装成只有CURD的操作,单例DB类.

三 实现代码

 把我的完全代码贴在这里了

  1 <?php
  2     /**
  3      * @ file base model class
  4      * @ author yuxing@sina.book.com
  5      */
  6 class sina_model{
  7 
  8     private $lastSql= '';
  9     private $optarr = array(
 10         'table'=>'',
 11         'field'=>'',
 12         'where'=>'',
 13         'limit'=>'',
 14     );
 15     public $conn;
 16     static $instance = null;
 17     private $_config=array();
 18     public function __construct($config=array())
 19     {
 20         if(is_array($config) &&!empty($config)){
 21             $this->_config= array_merge($this->_config,$config);
 22         }
 23     }
 24 
 25 
 26     /**
 27      * @ 单例模式获取DB类
 28      * @ 实例化db类,不一定要连接数据库
 29      */
 30     public static function getInstance(){
 31         if(is_null(self::$instance)){
 32             self::$instance = new sina_model();
 33         }
 34         return self::$instance;
 35     }
 36 
 37     /**
 38      * @ 连接数据库
 39      */
 40     public function connect($config=null){
 41         if(is_null($config)){
 42             die('没有数据库连接参数');
 43         }
 44         $this->conn = @mysql_connect($config['host'],$config['user'],$config['pwd']);
 45         if(is_null($this->conn)){
 46             die(@mysql_error());
 47         }
 48         return $this;
 49     }
 50 
 51     /**
 52      * @ 选择数据库
 53      */
 54     public function selectDb($config=null){
 55         $config = array_merge($config,$this->_config);
 56         if(is_null($config['dbname'])){
 57             exit('数据库不能为空');
 58         }
 59         @mysql_select_db($config['dbname'],$this->conn);
 60         return $this;
 61     }
 62 
 63     /**
 64      * @ 构造SQL 语句
 65      * @ 链式操作
 66      */
 67     // SELECT || INSERT || UPDATE || DELETE ||
 68 
 69     public function __call($name, $args)
 70     {
 71         // TODO: Implement __call() method.
 72         // $name 为方法名
 73         // $args 为参数
 74         if(array_key_exists($name,$this->optarr)){
 75             $this->optarr[$name] = $args['0'];
 76         }
 77         return $this; //返回自身,用于链式
 78     }
 79 
 80     /**
 81      * @ func 获取上次执行的sql语句.
 82      */
 83     public function getLastSql(){
 84         return $this->lastSql;
 85     }
 86 
 87     /**
 88      * @ func 查询sql 语句
 89      */
 90     public function select(){
 91         $sql = "SELECT ". $this->optarr['field']." FROM {$this->optarr['table']} WHERE {$this->optarr['where']} ";
 92         if(!empty($this->optarr['limit'])){
 93             $sql .=" LIMIT {$this->optarr['limit']}";
 94         }
 95         $this->lastSql = $sql;
 96         $res = @mysql_query($sql,$this->conn);
 97         return $res;
 98     }
 99 
100     /**
101      * @ update 操作
102      */
103     public function update (){
104 
105     }
106     /**
107      * @ func insert 语句
108      */
109 
110     /**
111      * @ func change array to string
112      */
113     public function arr2str($arr)
114     {
115         $res = is_array($arr) ? implode(',',$arr) : $arr;
116         return $res;
117     }
118     /**
119      *
120      */
121 }

使用方法. 在其他地方引入,本人在Controller引入

1 require_once(SYS_PATH.'core'.DIRECTORY_SEPARATOR.'sina_model'.EXT);

Controller 代码里面调用

 1         $db=new sina_model();
 2         $db = $db::getInstance();
 3         $config=array(
 4             'host'=>'127.0.0.1',
 5             'user'=>'root',
 6             'pwd'=>'root',
 7             'dbname'=>'test'
 8         );
 9 
10         $res =$db->connect($config)->selectDb($config)->table('user')->field('id')->where('id =1')->select();
11 //        $db->selectDb($config);
12         $row = @mysql_fetch_assoc($res);
13         var_dump($row);

我的数据库配置:

1 mysql  -uroot -p 
2 ****
3 create database test;
4 use test;
5 create table user(id int(1) not null);
6 insert user(id) value(1);

url 访问展示

OK done!!!

 

 

 

最后,本站所有博文皆为原创,转载请标明出处.小三爷这里谢过~~!

posted on 2015-12-03 14:30  任城三爷  阅读(461)  评论(0编辑  收藏  举报