Model.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. /**
  12. * ThinkPHP 简洁模式Model模型类
  13. * 只支持原生SQL操作 支持多数据库连接和切换
  14. */
  15. class Model {
  16. // 当前数据库操作对象
  17. protected $db = null;
  18. // 数据表前缀
  19. protected $tablePrefix = '';
  20. // 模型名称
  21. protected $name = '';
  22. // 数据库名称
  23. protected $dbName = '';
  24. // 数据表名(不包含表前缀)
  25. protected $tableName = '';
  26. // 实际数据表名(包含表前缀)
  27. protected $trueTableName ='';
  28. // 最近错误信息
  29. protected $error = '';
  30. /**
  31. * 架构函数
  32. * 取得DB类的实例对象
  33. * @param string $name 模型名称
  34. * @access public
  35. */
  36. public function __construct($name='') {
  37. // 模型初始化
  38. $this->_initialize();
  39. // 获取模型名称
  40. if(!empty($name)) {
  41. $this->name = $name;
  42. }elseif(empty($this->name)){
  43. $this->name = $this->getModelName();
  44. }
  45. // 数据库初始化操作
  46. // 获取数据库操作对象
  47. // 当前模型有独立的数据库连接信息
  48. $this->db(0,empty($this->connection)?$connection:$this->connection);
  49. // 设置表前缀
  50. $this->tablePrefix = $this->tablePrefix?$this->tablePrefix:C('DB_PREFIX');
  51. }
  52. // 回调方法 初始化模型
  53. protected function _initialize() {}
  54. /**
  55. * SQL查询
  56. * @access public
  57. * @param mixed $sql SQL指令
  58. * @return array
  59. */
  60. public function query($sql) {
  61. if(is_array($sql)) {
  62. return $this->patchQuery($sql);
  63. }
  64. if(!empty($sql)) {
  65. if(strpos($sql,'__TABLE__')) {
  66. $sql = str_replace('__TABLE__',$this->getTableName(),$sql);
  67. }
  68. return $this->db->query($sql);
  69. }else{
  70. return false;
  71. }
  72. }
  73. /**
  74. * 执行SQL语句
  75. * @access public
  76. * @param string $sql SQL指令
  77. * @return false | integer
  78. */
  79. public function execute($sql='') {
  80. if(!empty($sql)) {
  81. if(strpos($sql,'__TABLE__')) {
  82. $sql = str_replace('__TABLE__',$this->getTableName(),$sql);
  83. }
  84. $result = $this->db->execute($sql);
  85. return $result;
  86. }else {
  87. return false;
  88. }
  89. }
  90. /**
  91. * 得到当前的数据对象名称
  92. * @access public
  93. * @return string
  94. */
  95. public function getModelName() {
  96. if(empty($this->name)) {
  97. $this->name = substr(get_class($this),0,-5);
  98. }
  99. return $this->name;
  100. }
  101. /**
  102. * 得到完整的数据表名
  103. * @access public
  104. * @return string
  105. */
  106. public function getTableName() {
  107. if(empty($this->trueTableName)) {
  108. $tableName = !empty($this->tablePrefix) ? $this->tablePrefix : '';
  109. if(!empty($this->tableName)) {
  110. $tableName .= $this->tableName;
  111. }else{
  112. $tableName .= parse_name($this->name);
  113. }
  114. $this->trueTableName = strtolower($tableName);
  115. }
  116. return (!empty($this->dbName)?$this->dbName.'.':'').$this->trueTableName;
  117. }
  118. /**
  119. * 启动事务
  120. * @access public
  121. * @return void
  122. */
  123. public function startTrans() {
  124. $this->commit();
  125. $this->db->startTrans();
  126. return ;
  127. }
  128. /**
  129. * 提交事务
  130. * @access public
  131. * @return boolean
  132. */
  133. public function commit() {
  134. return $this->db->commit();
  135. }
  136. /**
  137. * 事务回滚
  138. * @access public
  139. * @return boolean
  140. */
  141. public function rollback() {
  142. return $this->db->rollback();
  143. }
  144. /**
  145. * 切换当前的数据库连接
  146. * @access public
  147. * @param integer $linkNum 连接序号
  148. * @param mixed $config 数据库连接信息
  149. * @return Model
  150. */
  151. public function db($linkNum='',$config=''){
  152. if(''===$linkNum && $this->db) {
  153. return $this->db;
  154. }
  155. static $_linkNum = array();
  156. static $_db = array();
  157. if(!isset($_db[$linkNum]) || (isset($_db[$linkNum]) && $_linkNum[$linkNum]!=$config) ) {
  158. // 创建一个新的实例
  159. if(!empty($config) && is_string($config) && false === strpos($config,'/')) { // 支持读取配置参数
  160. $config = C($config);
  161. }
  162. $_db[$linkNum] = Db::getInstance($config);
  163. }elseif(NULL === $config){
  164. $_db[$linkNum]->close(); // 关闭数据库连接
  165. unset($_db[$linkNum]);
  166. return ;
  167. }
  168. // 记录连接信息
  169. $_linkNum[$linkNum] = $config;
  170. // 切换数据库连接
  171. $this->db = $_db[$linkNum];
  172. return $this;
  173. }
  174. }