model.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. /**
  3. * ZenTaoPHP的baseModel类。
  4. * The baseModel class file of ZenTaoPHP framework.
  5. *
  6. * @package framework
  7. *
  8. * The author disclaims copyright to this source code. In place of
  9. * a legal notice, here is a blessing:
  10. *
  11. * May you do good and not evil.
  12. * May you find forgiveness for yourself and forgive others.
  13. * May you share freely, never taking more than you give.
  14. */
  15. class baseModel
  16. {
  17. /**
  18. * 全局对象$app。
  19. * The global $app object.
  20. *
  21. * @var object
  22. * @access public
  23. */
  24. public $app;
  25. /**
  26. * 应用名称$appName。
  27. * The global appName.
  28. *
  29. * @var string
  30. * @access public
  31. */
  32. public $appName;
  33. /**
  34. * 全局对象$config。
  35. * The global $config object.
  36. *
  37. * @var object
  38. * @access public
  39. */
  40. public $config;
  41. /**
  42. * 全局对象$lang。
  43. * The global $lang object.
  44. *
  45. * @var object
  46. * @access public
  47. */
  48. public $lang;
  49. /**
  50. * 全局对象$dbh,数据库连接句柄。
  51. * The global $dbh object, the database connection handler.
  52. *
  53. * @var object
  54. * @access public
  55. */
  56. public $dbh;
  57. /**
  58. * $dao对象,用于访问或者更新数据库。
  59. * The $dao object, used to access or update database.
  60. *
  61. * @var dao
  62. * @access public
  63. */
  64. public $dao;
  65. /**
  66. * $post对象,用于访问$_POST变量。
  67. * The $post object, used to access the $_POST var.
  68. *
  69. * @var object
  70. * @access public
  71. */
  72. public $post;
  73. /**
  74. * $get对象,用于访问$_GET变量。
  75. * The $get object, used to access the $_GET var.
  76. *
  77. * @var object
  78. * @access public
  79. */
  80. public $get;
  81. /**
  82. * $session对象,用于访问$_SESSION变量。
  83. * The $session object, used to access the $_SESSION var.
  84. *
  85. * @var object
  86. * @access public
  87. */
  88. public $session;
  89. /**
  90. * $server对象,用于访问$_SERVER变量。
  91. * The $server object, used to access the $_SERVER var.
  92. *
  93. * @var object
  94. * @access public
  95. */
  96. public $server;
  97. /**
  98. * $cookie对象,用于访问$_COOKIE变量。
  99. * The $cookie object, used to access the $_COOKIE var.
  100. *
  101. * @var object
  102. * @access public
  103. */
  104. public $cookie;
  105. /**
  106. * $global对象,用于访问$_GLOBAL变量。
  107. * The $global object, used to access the $_GLOBAL var.
  108. *
  109. * @var object
  110. * @access public
  111. */
  112. public $global;
  113. /**
  114. * $mao对象,用于访问缓存。
  115. * The $mao object, used to access the cache.
  116. *
  117. * @var object
  118. * @access public
  119. */
  120. public $mao;
  121. /**
  122. * 构造方法。
  123. * 1. 将全局变量设为model类的成员变量,方便model的派生类调用;
  124. * 2. 设置$config, $lang, $dbh, $dao。
  125. *
  126. * The construct function.
  127. * 1. global the global vars, refer them by the class member such as $this->app.
  128. * 2. set the paths, config, lang of current module
  129. *
  130. * @param string $appName
  131. * @access public
  132. * @return void
  133. */
  134. public function __construct($appName = '')
  135. {
  136. global $app, $config, $lang, $dbh, $dao;
  137. $this->app = $app;
  138. $this->config = $config;
  139. $this->lang = $lang;
  140. $this->dbh = $dbh;
  141. $this->dao = $dao;
  142. $this->mao = $app->mao;
  143. $this->appName = empty($appName) ? $this->app->getAppName() : $appName;
  144. $moduleName = $this->getModuleName();
  145. if($this->config->framework->multiLanguage) $this->app->loadLang($moduleName, $this->appName);
  146. if($moduleName != 'common') $this->app->loadModuleConfig($moduleName, $this->appName);
  147. $this->setSuperVars();
  148. /**
  149. * 读取当前模块的tao类。
  150. * Load the tao file auto.
  151. */
  152. $taoClass = $moduleName . 'Tao';
  153. $selfClass = static::class;
  154. $parentClasses = class_parents($this);
  155. if($selfClass != $taoClass && !isset($parentClasses[$taoClass])) $this->loadTao($moduleName, $this->appName);
  156. }
  157. /**
  158. * 获取该model的模块名,而不是用户请求的模块名。
  159. *
  160. * 这个方法通过去掉该model类名的'ext'和'model'字符串,来获取当前模块名。
  161. * 不要使用$app->getModuleName(),因为其返回的是用户请求的模块名。
  162. * 另一个model可以通过loadModel()加载进来,与请求的模块名不一致。
  163. *
  164. * Get the module name of this model. Not the module user visiting.
  165. *
  166. * This method replace the 'ext' and 'model' string from the model class name, thus get the module name.
  167. * Not using $app->getModuleName() because it return the module user is visiting. But one module can be
  168. * loaded by loadModel() so we must get the module name of this model.
  169. *
  170. * @access public
  171. * @return string the module name.
  172. */
  173. public function getModuleName()
  174. {
  175. $className = static::class;
  176. $parentClasses = class_parents($this);
  177. if(count($parentClasses) > 2) $className = current(array_slice($parentClasses, -3, 1));
  178. if(strtolower(substr($className, -5)) == 'model') $className = strtolower(substr($className, 0, strlen($className) - 5));
  179. return $className;
  180. }
  181. /**
  182. * 设置全局超级变量。
  183. * Set the super vars.
  184. *
  185. * @access public
  186. * @return void
  187. */
  188. public function setSuperVars()
  189. {
  190. $this->post = $this->app->post;
  191. $this->get = $this->app->get;
  192. $this->server = $this->app->server;
  193. $this->cookie = $this->app->cookie;
  194. $this->session = $this->app->session;
  195. }
  196. /**
  197. * 加载一个模块的model对象。加载完成后,使用$this->$moduleName来访问这个model对象。
  198. * 比如:loadModel('user')引入user模块的model实例对象,可以通过$this->user来访问它。
  199. *
  200. * Load the model object of one module. After loaded, can use $this->$moduleName to visit the model object.
  201. *
  202. * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name.
  203. * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name.
  204. * @access public
  205. * @return object|bool 如果没有model文件,返回false,否则返回model对象。If no model file, return false, else return the model object.
  206. */
  207. public function loadModel($moduleName, $appName = '')
  208. {
  209. $model = $this->app->loadTarget($moduleName, $appName);
  210. if(!$model) return false;
  211. $this->{$moduleName} = $model;
  212. return $model;
  213. }
  214. /**
  215. * 加载一个模块的tao对象。加载完成后,使用$this->{$moduleName}Tao来访问这个tao对象。
  216. * 比如:loadTao('user')引入user模块的tao实例对象,可以通过$this->userTao来访问它。
  217. *
  218. * Load the tao object of one module. After loaded, can use $this->{$moduleName}Tao to visit the tao object.
  219. *
  220. * @param string $moduleName 模块名,如果为空,使用当前模块。The module name, if empty, use current module's name.
  221. * @param string $appName 应用名,如果为空,使用当前应用。The app name, if empty, use current app's name.
  222. * @access public
  223. * @return object|bool 如果没有tao文件,返回false,否则返回tao对象。If no tao file, return false, else return the tao object.
  224. */
  225. public function loadTao($moduleName, $appName = '')
  226. {
  227. $tao = $this->app->loadTarget($moduleName, $appName, 'tao');
  228. if(!$tao) return false;
  229. $taoObjectName = $moduleName . 'Tao';
  230. $this->{$taoObjectName} = $tao;
  231. return $tao;
  232. }
  233. /**
  234. * 加载model的class扩展,主要是为了开发加密代码使用。
  235. * 可以将主要的逻辑存放到$moduleName/ext/model/class/$extensionName.class.php中。
  236. * 然后在ext/model/$extension.php的扩展里面使用$this->loadExtension()来调用相应的方法。
  237. * ext/model/class/*.class.php代码可以加密。而ext/model/*.php可以不用加密。
  238. * 因为框架对model的扩展是采取合并文件的方式,ext/model/*.php文件不能加密。
  239. *
  240. * Load extension class of a model thus user can encrypt the code.
  241. * You can put the main extension logic codes in $moduleName/ext/model/class/$extensionName.class.php.
  242. * And call them by the ext/model/$extension.php like this: $this->loadExtension('myextension')->method().
  243. * You can encrypt the code in ext/model/class/*.class.php.
  244. * Because the framework will merge the extension files in ext/model/*.php to the module/model.php.
  245. *
  246. * @param string $extensionName
  247. * @param string $moduleName
  248. * @access public
  249. * @return mixed
  250. */
  251. public function loadExtension($extensionName, $moduleName = '')
  252. {
  253. if(empty($extensionName)) return false;
  254. if(empty($moduleName)) $moduleName = $this->getModuleName();
  255. $moduleName = strtolower($moduleName);
  256. $extensionName = strtolower($extensionName);
  257. $type = 'model';
  258. $className = strtolower(static::class);
  259. if($className == $moduleName . 'tao' || $className == 'ext' . $moduleName . 'tao') $type = 'tao';
  260. /* 设置扩展类的名字。Set the extension class name. */
  261. $extensionClass = $extensionName . ucfirst($moduleName);
  262. if($type != 'model') $extensionClass .= ucfirst($type);
  263. if(isset($this->$extensionClass)) return $this->$extensionClass;
  264. /* 设置扩展的名字和相应的文件。Set extenson name and extension file. */
  265. $moduleExtPath = $this->app->getModuleExtPath($moduleName, $type);
  266. if(!empty($moduleExtPath['site'])) $extensionFile = $moduleExtPath['site'] . 'class/' . $extensionName . '.class.php';
  267. if(!isset($extensionFile) or !file_exists($extensionFile)) $extensionFile = $moduleExtPath['saas'] . 'class/' . $extensionName . '.class.php';
  268. if(!isset($extensionFile) or !file_exists($extensionFile)) $extensionFile = $moduleExtPath['custom'] . 'class/' . $extensionName . '.class.php';
  269. if(!isset($extensionFile) or !file_exists($extensionFile)) $extensionFile = $moduleExtPath['vision'] . 'class/' . $extensionName . '.class.php';
  270. if(!isset($extensionFile) or !file_exists($extensionFile)) $extensionFile = $moduleExtPath['xuan'] . 'class/' . $extensionName . '.class.php';
  271. if(!isset($extensionFile) or !file_exists($extensionFile)) $extensionFile = $moduleExtPath['common'] . 'class/' . $extensionName . '.class.php';
  272. /* 载入父类。Try to import parent model file auto and then import the extension file. */
  273. if(!class_exists($moduleName . ucfirst($type))) helper::import($this->app->getModulePath($this->appName, $moduleName) . $type . '.php');
  274. if(!helper::import($extensionFile)) return false;
  275. if(!class_exists($extensionClass)) return false;
  276. /* 实例化扩展类。Create an instance of the extension class and return it. */
  277. $extensionObject = new $extensionClass();
  278. if($type == 'model') $extensionClass = str_replace(ucfirst($type), '', $extensionClass);
  279. $this->$extensionClass = $extensionObject;
  280. return $extensionObject;
  281. }
  282. /**
  283. * 删除记录。
  284. * Delete one record.
  285. *
  286. * @param string $table the table name
  287. * @param int $id the id value of the record to be deleted
  288. * @access public
  289. * @return void
  290. */
  291. public function delete($table, $id)
  292. {
  293. $this->dao->delete()->from($table)->where('id')->eq($id)->exec();
  294. }
  295. }