转自: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类。

phpcms V9 数据模型基类(转)的更多相关文章

  1. phpcms V9 数据模型基类

    在学习<phpcms V9首页模板文件解析>的第七步,我们看到content_model类,文件路径:phpcms/model/content_model.class.php 从代码中,可 ...

  2. PHPCMS V9 模块开发 二次开发实例 留言本

    鄙人实现了PHPCMS V9 产品开发权威指南(2011官方最新版).doc中的留言板实例,并加上模块安装和卸载功能, 程序可以运行,但只实现基本功能,目的是想让和我一样徘徊在PHPCMS门口不知道从 ...

  3. phpcms v9二次开发之数据模型类

    系统模型类:model.class.php数据模型类的位置:/phpcms/libs/classes phpcms v9二次开发中,我们要经常需要对模块的数据表进行查询.添加.修改和删除数据等操作,所 ...

  4. phpcms v9二次开发之模型类的应用(1)

    在<phpcms二次开发之模型类model.class.php>中讲到了模型类的建立方法,接下来我讲一下模型类的应用.      前段时间我基于phpcms v9开发了一个足球网.足球网是 ...

  5. phpcms v9二次开发之模型类的应用(2)

    二.模型操作方法select()--查询语句         //查询级别管理列表信息    public function levellists() { $lelists = $this->l ...

  6. phpcms V9 添加模块

    为phpcms创建一个模块的开发流程 [1]创建模块目录 通过前面的学习,我们已经知道phpcms V9框架中的模块位于phcms/modules目录中,每一个目录称之为一个模块. 如果要创建一个模块 ...

  7. phpcms V9 添加模块(转)

    转自:http://www.cnblogs.com/Braveliu/p/5101345.html 为phpcms创建一个模块的开发流程 [1]创建模块目录 通过前面的学习,我们已经知道phpcms ...

  8. phpcms V9 首页模板文件解析

    在了解了<phpcms V9 URL访问解析>之后,我们已经知道首页最终执行的是content模块下index控制器的init方法. 下面, 我们逐步分析过程如下: 第一.首页默认执行的是 ...

  9. PHPCMS V9 框架代码分析(入口程序)

    PHPCMS是采用MVC设计模式开发,基于模块和操作的方式进行访问,采用单一入口模式进行项目部署和访问,无论访问任何一个模块或者功能,只有一个统一的入口. 入口程序是在前期处理用户请求的引导程序.它是 ...

随机推荐

  1. poj 2484 A Funny Game(博弈)

    A Funny Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4639   Accepted: 2855 Desc ...

  2. 《算法问题实战策略》——chaper9——动态规划法技巧

    Q1: 数字游戏: 两个人(A.B)用n个整数排成的一排棋盘玩游戏,游戏从A开始,每个人有如下操作: (1)    拿走棋盘最右侧或者最左侧的棋子,被拿走的数字从棋盘中抹掉. (2)    棋盘中还剩 ...

  3. [2013 ACM/ICPC Asia Regional Hangzhou Online J/1010]hdu 4747 Mex (线段树)

    题意: + ;];;;], seg[rt <<  | ]);)) * fa.setv;) * fa.setv;;], seg[rt <<  | ], r - l + );;,  ...

  4. zoj 3656

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4879 代码: #include<cstdio> #inc ...

  5. 前端开发者应当了解的 Web 缓存知识

    缓存优点 通常所说的Web缓存指的是可以自动保存常见http请求副本的http设备.对于前端开发者来说,浏览器充当了重要角色.除此外常见的还有各种各样的代理服务器也可以做缓存.当Web请求到达缓存时, ...

  6. Struct2 向Action中传递参数(中文乱码问题)

    就是把视图上的值传递到Action定义的方法中 也就是把数据从前台传递到后台 三种方式: 1.  使用action属性接收参数 比如jsp页面: <body> 使用action属性接收参数 ...

  7. ural 1106. Two Teams 二分图染色

    链接:http://acm.timus.ru/problem.aspx?space=1&num=1106 描述:有n(n<=100)个人,每个人有一个或多个朋友(朋友关系是相互的).将其 ...

  8. 1242Rescue (优先队列BFS)

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...

  9. hdu1195 Open the Lock (DFS)

    Problem Description Now an emergent task for you is to open a password lock. The password is consist ...

  10. poj 1328贪心

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...