在學習《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 // 理解一點: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類。
Good Good Study, Day Day Up.
順序 選擇 循環 總結