我们看到content_model类,文件路径:phpcms/model/content_model.class.php从代码中,可以得知content_model类继承于model类。那么model类又是什么呢?
下面请看数据模型基类model类的解析。文件路径:phpcms\libs\classes\model.class.php
代码及注释,如下所示:
1 <?php
2
10 model.class.php
11 defined('IN_PHPCMS') or exit('Access Denied');
12 pc_base::load_sys_class('db_factory', '', 0);
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
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
58 }
59
60
70 final public function select($where = '', $data = '*', $limit = '', $order = '', $group = '', $key='')
71 {
72 if (is_array($where))
73 $where = $this->sqls($where);
74
75 return $this->db->select($data, $this->table_name, $where, $limit, $order, $group, $key);
76 }
77
78
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
112 final public function get_one($where = '', $data = '*', $order = '', $group = '')
113 {
114 if (is_array($where))
115 $where = $this->sqls($where);
116
117 return $this->db->get_one($data, $this->table_name, $where, $order, $group);
118 }
119
120
125 final public function query($sql)
126 {
127 $sql = str_replace('phpcms_', $this->db_tablepre, $sql);
128
129 return $this->db->query($sql);
130 }
131
132
139 final public function insert($data, $return_insert_id = false, $replace = false)
140 {
141
142 return $this->db->insert($data, $this->table_name, $return_insert_id, $replace);
143 }
144
145
149 final public function insert_id()
150 {
151
152 return $this->db->insert_id();
153 }
154
155
165 final public function update($data, $where = '')
166 {
167 if (is_array($where))
168 $where = $this->sqls($where);
169
170 return $this->db->update($data, $this->table_name, $where);
171 }
172
173
178 final public function delete($where)
179 {
180 if (is_array($where))
181 $where = $this->sqls($where);
182
183 return $this->db->delete($this->table_name, $where);
184 }
185
186
190 final public function count($where = '')
191 {
192 $r = $this->get_one($where, "COUNT(*) AS num");
193 return $r['num'];
194 }
195
196
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
222 }
223 else
224 {
225 return $where;
226 }
227 }
228
229
233 final public function affected_rows()
234 {
235
236 return $this->db->affected_rows();
237 }
238
239
243 final public function get_primary()
244 {
245
246 return $this->db->get_primary($this->table_name);
247 }
248
249
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
265 return $this->db->get_fields($table_name);
266 }
267
268
273 final public function table_exists($table)
274 {
275 return $this->db->table_exists($this->db_tablepre.$table);
276 }
277
278
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
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
311 final public function version()
312 {
313 return $this->db->version();
314 }
315 }
备注:phpcms/model文件夹下的所有文件中的类都继承于这个model类。
本文标签:
声明:本文由代码号注册/游客用户【冷卉】供稿发布,本站不对用户发布的phpcmsV9数据模型基类解析信息内容原创度和真实性等负责。如内容侵犯您的版权或其他权益,请留言并加以说明。站长审查之后若情况属实会及时为您删除。同时遵循 CC 4.0 BY-SA 版权协议,尊重和保护作者的劳动成果,转载请标明出处链接和本声明内容。本文作者:冷卉» https://www.ebingou.cn/dmh/15883.html
很赞哦! (1)