您的位置:首页 > 编程语言 > PHP开发

phpcms V9 数据模型基类(转)

2016-06-27 13:38 381 查看
转自:http://www.cnblogs.com/Braveliu/p/5100421.html

在学习《phpcms V9首页模板文件解析》的第七步,我们看到content_model类,文件路径:phpcms/model/content_model.class.php

从代码中,可以得知content_model类继承于model类。那么model类又是什么呢?

下面请看数据模型基类model类的解析。文件路径:phpcms\libs\classes\model.class.php

代码及注释,如下所示:




1 <?php
2 /**
3  *  model.class.php 数据模型基类
4  *  文件路径:phpcms/libs/classes/model.class.php
5  *
6  * @copyright            (C) 2005-2010 PHPCMS
7  * @license                http://www.phpcms.cn/license/ 8  * @lastmodify            2010-6-7
9  */
10  model.class.php// 理解一点:phpcms/model文件夹下的所有文件的model子类都继承于这个model
11 defined('IN_PHPCMS') or exit('Access Denied');
12 pc_base::load_sys_class('db_factory', '', 0); // 数据库工厂类,路径:phpcms/libs/classes/db_factory.class.php
13
14 class model
15 {
16     //数据库配置
17     protected $db_config = '';
18     //数据库连接
19     protected $db = '';
20     //调用数据库的配置项
21     protected $db_setting = 'default';
22     //数据表名
23     protected $table_name = '';
24     //表前缀
25     public  $db_tablepre = '';
26     //构造函数
27     public function __construct()
28     {
29         if (!isset($this->db_config[$this->db_setting]))
30         {
31             $this->db_setting = 'default';
32         }
33        /**
34         * $this->db_config['default'];相当于caches/configs/database.php文件返回的数组
35         * return array (
36             'default' => array (
37                 'hostname' => 'localhost', // 主机名
38                 'port' => 3306,        // 端口
39                 'database' => 'phpcmsv9', // 数据库名
40                 'username' => 'root',    // 数据库用户名
41                 'password' => '',        // 数据库密码
42                 'tablepre' => 'v9_',    // 数据表前缀
43                 'charset' => 'gbk',        // 数据库字符编码
44                 'type' => 'mysql',        // 数据库类型:例如:mysql、access等等,根据数据库类型不同加载不同的数据库驱动
45                 'debug' => true,
46                 'pconnect' => 0,
47                 'autoconnect' => 0 ),
48                 );
49         **/
50         $this->table_name = $this->db_config[$this->db_setting]['tablepre'].$this->table_name;
51         $this->db_tablepre = $this->db_config[$this->db_setting]['tablepre'];
52         $this->db = db_factory::get_instance($this->db_config)->get_database($this->db_setting);
53         /**
54          * 1.db_factory工厂类主要采用单例模式返回一个唯一的数据库连接实例对象
55          * 2.db_factory工厂类在实例化数据库实例时,会根据当前数据库的type,加载不同的数据库驱动,返回不同的数据库实例对象
56          * 3.db_factory工厂类通过get_instance方法从caches/configs/database.php文件中获取数据库配置信息
57          **/
58     }
59
60     /**
61      * 执行sql查询
62      * @param $where         查询条件[例`name`='$name']
63      * @param $data         需要查询的字段值[例`name`,`gender`,`birthday`]
64      * @param $limit         返回结果范围[例:10或10,10 默认为空]
65      * @param $order         排序方式    [默认按数据库默认方式排序]
66      * @param $group         分组方式    [默认为空]
67      * @param $key          返回数组按键名排序
68      * @return array        查询结果集数组
69      */
70     final public function select($where = '', $data = '*', $limit = '', $order = '', $group = '', $key='')
71     {
72         if (is_array($where))
73             $where = $this->sqls($where);
74         //如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的select方法
75         return $this->db->select($data, $this->table_name, $where, $limit, $order, $group, $key);
76     }
77
78     /**
79      * 查询多条数据并分页
80      * @param $where
81      * @param $order
82      * @param $page
83      * @param $pagesize
84      * @return unknown_type
85      */
86     final public function listinfo($where = '', $order = '', $page = 1, $pagesize = 20, $key='', $setpages = 10,$urlrule = '',$array = array(), $data = '*')
87     {
88         $where = to_sqls($where);
89         $this->number = $this->count($where);
90         $page = max(intval($page), 1);
91         $offset = $pagesize*($page-1);
92         $this->pages = pages($this->number, $page, $pagesize, $urlrule, $array, $setpages);
93         $array = array();
94         if ($this->number > 0)
95         {
96             return $this->select($where, $data, "$offset, $pagesize", $order, '', $key);
97         }
98         else
99         {
100             return array();
101         }
102     }
103
104     /**
105      * 获取单条记录查询
106      * @param $where         查询条件
107      * @param $data         需要查询的字段值[例`name`,`gender`,`birthday`]
108      * @param $order         排序方式    [默认按数据库默认方式排序]
109      * @param $group         分组方式    [默认为空]
110      * @return array/null    数据查询结果集,如果不存在,则返回空
111      */
112     final public function get_one($where = '', $data = '*', $order = '', $group = '')
113     {
114         if (is_array($where))
115             $where = $this->sqls($where);
116         //如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的get_one方法
117         return $this->db->get_one($data, $this->table_name, $where, $order, $group);
118     }
119
120     /**
121      * 直接执行sql查询
122      * @param $sql                            查询sql语句
123      * @return    boolean/query resource        如果为查询语句,返回资源句柄,否则返回true/false
124      */
125     final public function query($sql)
126     {
127         $sql = str_replace('phpcms_', $this->db_tablepre, $sql);
128         //如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的query方法
129         return $this->db->query($sql);
130     }
131
132     /**
133      * 执行添加记录操作
134      * @param $data         要增加的数据,参数为数组。数组key为字段值,数组值为数据取值
135      * @param $return_insert_id 是否返回新建ID号
136      * @param $replace 是否采用 replace into的方式添加数据
137      * @return boolean
138      */
139     final public function insert($data, $return_insert_id = false, $replace = false)
140     {
141         //如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的insert方法
142         return $this->db->insert($data, $this->table_name, $return_insert_id, $replace);
143     }
144
145     /**
146      * 获取最后一次添加记录的主键号
147      * @return int
148      */
149     final public function insert_id()
150     {
151         //如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的insert_id方法
152         return $this->db->insert_id();
153     }
154
155     /**
156      * 执行更新记录操作
157      * @param $data         要更新的数据内容,参数可以为数组也可以为字符串,建议数组。
158      *                         为数组时数组key为字段值,数组值为数据取值
159      *                         为字符串时[例:`name`='phpcms',`hits`=`hits`+1]。
160      *                        为数组时[例: array('name'=>'phpcms','password'=>'123456')]
161      *                        数组的另一种使用array('name'=>'+=1', 'base'=>'-=1');程序会自动解析为`name` = `name` + 1, `base` = `base` - 1
162      * @param $where         更新数据时的条件,可为数组或字符串
163      * @return boolean
164      */
165     final public function update($data, $where = '')
166     {
167         if (is_array($where))
168             $where = $this->sqls($where);
169         //如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的update方法
170         return $this->db->update($data, $this->table_name, $where);
171     }
172
173     /**
174      * 执行删除记录操作
175      * @param $where         删除数据条件,不充许为空。
176      * @return boolean
177      */
178     final public function delete($where)
179     {
180         if (is_array($where))
181             $where = $this->sqls($where);
182         //如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的delete方法
183         return $this->db->delete($this->table_name, $where);
184     }
185
186     /**
187      * 计算记录数
188      * @param string/array $where 查询条件
189      */
190     final public function count($where = '')
191     {
192         $r = $this->get_one($where, "COUNT(*) AS num");
193         return $r['num'];
194     }
195
196     /**
197      * 将数组转换为SQL语句
198      * @param array $where 要生成的数组
199      * @param string $font 连接串。
200      */
201     final public function sqls($where, $font = ' AND ')
202     {
203         if (is_array($where))
204         {
205             $sql = '';
206             foreach ($where as $key=>$val)
207             {
208                 $sql .= $sql ? " $font `$key` = '$val' " : " `$key` = '$val'";
209             }
210             return $sql;
211             /**
212             *    foreach 可以遍历数据与对象,它会把当前单元(元素)的键名在每次循环中被赋给变量$key,值赋给变量$val。
213             *   $row=array('one'=>1,'two'=>2);
214             *   foreach($row as $key=>$val)
215             *    {
216             *        echo $key.'--'.$val;
217             *    }
218             *    执行结果:
219             *   第一次遍历的$key是one,$val是1;
220             *    第二次遍历的$key是two,$val是2;
221             **/
222         }
223         else
224         {
225             return $where;
226         }
227     }
228
229     /**
230      * 获取最后数据库操作影响到的条数
231      * @return int
232      */
233     final public function affected_rows()
234     {
235         //如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的affected_rows方法
236         return $this->db->affected_rows();
237     }
238
239     /**
240      * 获取数据表主键
241      * @return array
242      */
243     final public function get_primary()
244     {
245         //如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的get_primary方法
246         return $this->db->get_primary($this->table_name);
247     }
248
249     /**
250      * 获取表字段
251      * @param string $table_name    表名
252      * @return array
253      */
254     final public function get_fields($table_name = '')
255     {
256         if (empty($table_name))
257         {
258             $table_name = $this->table_name;
259         }
260         else
261         {
262             $table_name = $this->db_tablepre.$table_name;
263         }
264         //如果加载的数据库驱动类型是mysql,则会调用phpcms/libs/classes/mysql.class.php数据库驱动文件类库的get_fields方法
265         return $this->db->get_fields($table_name);
266     }
267
268     /**
269      * 检查表是否存在
270      * @param $table 表名
271      * @return boolean
272      */
273     final public function table_exists($table)
274     {
275         return $this->db->table_exists($this->db_tablepre.$table);
276     }
277
278     /**
279      * 检查字段是否存在
280      * @param $field 字段名
281      * @return boolean
282      */
283     public function field_exists($field)
284     {
285         $fields = $this->db->get_fields($this->table_name);
286         return array_key_exists($field, $fields);
287     }
288
289     final public function list_tables()
290     {
291         return $this->db->list_tables();
292     }
293     /**
294      * 返回数据结果集
295      * @param $query (mysql_query返回值)
296      * @return array
297      */
298     final public function fetch_array()
299     {
300         $data = array();
301         while($r = $this->db->fetch_next())
302         {
303             $data[] = $r;
304         }
305         return $data;
306     }
307
308     /**
309      * 返回数据库版本号
310      */
311     final public function version()
312     {
313         return $this->db->version();
314     }
315 }





备注:phpcms/model文件夹下的所有文件中的类都继承于这个model类。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: