PHPCMS V9 Data Model base class (RPM)

Source: Internet
Author: User
Tags php class php database

Transferred from: http://www.cnblogs.com/Braveliu/p/5100421.html

In learning "phpcms V9 Home template file parsing," The seventh step, we see Content_model class, File path: phpcms/model/content_model.class.php

From the code, you know that the Content_model class inherits from the model class. So what is the model class?

The following is a look at the parsing of the base class model class for data models. File path: phpcms\libs\classes\model.class.php

The code and comments are as follows:

  1 <?php 2/** 3 * model.class.php Data Model base class 4 * File path: 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 * * model.class.php//understand one thing: the model subclass of all files under the Phpcms/model folder inherits from this model defined (' In_phpcms ') or exit (' Acces S Denied '); Pc_base::load_sys_class (' db_factory ', ', 0); Database factory class, path: phpcms/libs/classes/db_factory.class.php class Model 15 {16///Database configuration protected $db _con Fig = "; 18//Database connection protected $db = '; 20//Call the database configuration item protected $db _setting = ' default '; 22//Data table name: protected $table _name = "; 24//Table prefix public $db _tablepre = '; 26//constructor for public function __construct () (!isset ($this->db_config[$this->db_settin G])) {$this->db_setting = ' default ';/** * $this->db_config[' default '; the equivalent of the array returned by the caches/configs/database.php file. * Return Array (+ ' default ' = = Array                 (PNS ' hostname ' = ' localhost ',//hostname ' port ' = 3306,//Port 39 ' Database ' = ' phpcmsv9 ',//DB name ' username ' + ' root ',//database user name ' PA ' ssWOrd ' + ',//Database password ' tablepre ' = ' v9_ ',//data table prefix ' CharSet ' =&gt ;                  ' GBK ',//database character encoded with ' type ' = ' mysql ',//database type: For example: MySQL, access, etc. load different database drivers depending on the type of database 45                  ' Debug ' = True, the ' pconnect ' + 0, AutoConnect ' + 0), 48 ); **/$this->table_name = $this->db_config[$this->db_setting][' Tablepre ']. $this->table_nam E Wuyi $this->db_tablepre = $this->db_config[$this->db_setting][' tablepre '; $this->db = Db_faCtory::get_instance ($this->db_config)->get_database ($this->db_setting); /** * 1.db_factory factory class mainly uses singleton mode to return a unique database connection instance object. * 2.db_factory factory class When instantiating a db instance, it is based on the current database          Type, loading different database drivers, returning different DB instance objects. * 3.db_factory factory class Get database configuration information from caches/configs/database.php files by Get_instance method 57      **/58} 59 60/** 61 * Execute SQL QUERY * @param $where query Criteria [example ' name ' = ' $name '] 63 * @param $data field values to query [example ' name ', ' gender ', ' birthday '] * @param $limit return result range [Example: 10 or 10, 10 default is empty] * * @param $order Sort By default [sorted by database by default] * * @param $group GROUP by default [empty] * @param $ke Y returns the array key name sort. * @return Array Query result set arrays */FINAL public Function Select ($where = ', $d ATA = ' * ', $limit = ', $order = ', $group = ', $key = ') * (Is_array ($where)) $whe Re = $this->sqls ($where); 74//If the loaded database driver type is MySQL, it will be calledPhpcms/libs/classes/mysql.class.php the Select method of the database driver file class library $this->db->select ($data, $this->table_n Ame, $where, $limit, $order, $group, $key);      76} 77 78/** 79 * Querying multiple data and pagination * @param $where Bayi * @param $order * * @param $page 83 * @param $pagesize * @return unknown_type * * Listinfo ($where = ", $order = ", $page = 1, $pagesize = A, $key =", $setpages = ten, $urlrule = ", $array = Array (), $data = ' * ') 87 {88 $ where = To_sqls ($where); $this->number = $this->count ($where); $page = Max (Intval ($page), 1); $offset = $pagesize * ($page-1); $this->pages = pages ($this->number, $page, $pagesize, $urlrule, $array, $setpages); $array = Array (); 94 if ($this->number > 0) $this->select ($where, $data, "$offset, $pag Esize ", $order, ', $key);         97} 98else}102 {return array (); 101}103 104/**105 * Get a single record query 106 * @param         $where Query Criteria 107 * @param $data field values to query [example ' name ', ' gender ', ' birthday ']108 * @param $order Sort by default by database]109 * @param $group GROUP by default [null]110 * @return array/null data Query result set, if not present, return null 11  1 */112 Final public function get_one ($where = ', $data = ' * ', $order = ', $group = ') 113 { (Is_array ($where)) $where = $this->sqls ($where); 116//If the loaded database driver type is MySQL, the phpcms/libs/class is called Get_one method of es/mysql.class.php database driver file class Library 117 return $this->db->get_one ($data, $this->table_name, $where, $ord      Er, $group); 118}119 120/**121 * Execute SQL query directly 122 * @param $sql query SQL statement 123 * @return Boolean/query Resource If you return a resource handle for a query statement, return true/false124 */125 final public Function query     ($sql) 126 {127    $sql = Str_replace (' Phpcms_ ', $this->db_tablepre, $sql); 128//If the loaded database driver type is MySQL, the phpcms/libs/classes/my is called Query method for sql.class.php database drive file class Library 129 return $this->db->query ($sql); 130}131 132/**133 * Execute add Record Operation 134 * @param $data the data to be added, the parameter is an array.      The array key is the field value, the array value is the data value 135 * @param $return _insert_id Returns the new ID number 136 * @param $replace whether to add data using replace into 137     * @return boolean138 */139 Final public Function insert ($data, $return _insert_id = False, $replace = False) 140 {141///If the loaded database-driven type is MySQL, the Insert method of the Phpcms/libs/classes/mysql.class.php database-driven file class library is called 142 return $this-&G T;db->insert ($data, $this->table_name, $return _insert_id, $replace); 143}144 145/**146 * Gets the last added record The primary Key number 147 * @return int 148 */149 final public Function insert_id () 150 {151//if the loaded database driver type is MySQL , the insert_id method that invokes the Phpcms/libs/classes/mysql.class.php database-driven file class library is called to return $this->db->iNSERT_ID (); 153}154 155/**156 * Perform an update record operation 157 * @param $data the data content to be updated, the parameter can be an array or a string, which is recommended. 158 * Array key is the field value when the array value is the data value 159 * is a string [example: ' Name ' = ' phpcms ', ' hits ' = ' hits ' +1].                        160 * For Array [Example: Array (' name ' = ' = ' phpcms ', ' password ' = ' 123456 ')]161 * An array of other uses array (' name ' = ' = ' +=1 ', ' base ' = '-=1 '); the program automatically resolves to ' name ' = ' name ' + 1, ' base ' = ' base '-1162 * @param $ The condition in where the data is updated, can be an array or string 163 * @return boolean164 */165 final public Function update ($data, $where = ' ') 166 {167 if (Is_array ($where)) 168 $where = $this->sqls ($where); 169//If the loaded database driver type is M Ysql, the Update method for the Phpcms/libs/classes/mysql.class.php database-driven file class library is called->db->update return $this ($data, $this-&gt ; table_name, $where); 171}172 173/**174 * Perform delete record operation 175 * @param $where Delete data condition, not empty. 176 * @return Boolean177 */178 Final Public Function Delete ($where) 179 {Is_array ($where)) 181 $where         = $this->sqls ($where); 182//If the loaded database driver type is MySQL, the phpcms/libs/classes/mysql.class.php database driver file class library is called by the Delete method 183      return $this->db->delete ($this->table_name, $where); 184}185 186/**187 * Count Records 188 * @param string/array $where query Condition 189 */190 Final Public function count ($where = ") 191 {192 $r = $th Is->get_one ($where, "COUNT (*) as num"); 193 return $r [' num '];194}195 196/**197 * Convert array to SQL statement 1 98 * @param array $where to be generated 199 * @param string $font connection string.         */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}211 return $sql;/**212 * foreach can traverse data and objects, it assigns the key name of the current cell (element) to the variable $key in each loop, assigning the value to the variable $ Val             213 * $row =array (' One ' =>1, ' one ' =>2); 214 * foreach ($row as $key + = $val) 215 * {216 * echo $key. ' --'. $val; 217 *}218 * Execution Result: 219 * First traversal of $key is one, $val is 1;220 * Second         The $key of the second traversal is two, $val is 2;221 **/222} 223 Else 224 {225 return $where; 226 }227}228 229/**230 * Gets the number of bars affected by the last database operation 231 * @return int232 */233 final public function A Ffected_rows () 234 {235///If the database driver type being loaded is MySQL, the Affected_rows method of the Phpcms/libs/classes/mysql.class.php database driver file class library is called 2 ->db->affected_rows return $this (); 237}238 239/**240 * Get data Table primary KEY 241 * @return Array2 */243 Final public Function get_primary () 244 {245//if the loaded database driver type is MySQL,The Get_primary method for the phpcms/libs/classes/mysql.class.php database driver file class library is called 246 return $this->db->get_primary ($this, table_name); 247}248 249/**250 * Get table field 251 * @param string $table _name table name 252 * @return Array         253 */254 Final Public Function get_fields ($table _name = ") 255 {if (empty ($table _name)) 257 {258 $table _name = $this->table_name;259} 260 Else 261 {262 $tabl E_name = $this->db_tablepre. $table _name;263}264//If the loaded database driver type is MySQL, it will be called PHPCMS/LIBS/CLASSES/MYSQL.CLA      Get_fields method of ss.php database driver file class Library 265 return $this->db->get_fields ($table _name); 266}267 268/**269 * Check if the table exists * @param $table table name 271 * @return boolean272 */273 final public function table_exists ($ta BLE) 274 {275 return $this->db->table_exists ($this->db_tablepre. $table); 276}277 278/**27      9 * Check if field exists 280* @param $field Field name 281 * @return boolean282 */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}29 3/**294 * Return data result set 295 * @param $query (mysql_query return value) 296 * @return array297 */298 final Publi         C function Fetch_array () 299 {$data = array (); 301 while ($r = $this->db->fetch_next ()) 302    {303 $data [] = $r; 304}305 return $data; 306}307 308/**309 * Returns the database version number 310 */311 final public funct Ion version () 312 {313 return $this->db->version (); 314}315}

Note: The classes in all files under the Phpcms/model folder inherit from this model class.

PHPCMS V9 Data Model base class (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.