app. * 2. set the pathes, config, lang of current module * * @param string $appName * @access public * @return void */ public function __construct($appName = '') { global $app, $config, $lang, $dbh; $this->app = $app; $this->config = $config; $this->lang = $lang; $this->dbh = $dbh; $this->appName = empty($appName) ? $this->app->getAppName() : $appName; $moduleName = $this->getModuleName(); $this->app->loadLang($moduleName, $this->appName); $this->app->loadConfig($moduleName, $this->appName, $exitIfNone = false); $this->loadDAO(); $this->setSuperVars(); } /** * 获取该model的模块名,而不是用户请求的模块名。 * * 这个方法通过去掉该model类名的'ext'和'model'字符串,来获取当前模块名。 * 不要使用$app->getModuleName(),因为其返回的是用户请求的模块名。 * 另一个model可以通过loadModel()加载进来,与请求的模块名不一致。 * * Get the module name of this model. Not the module user visiting. * * This method replace the 'ext' and 'model' string from the model class name, thus get the module name. * Not using $app->getModuleName() because it return the module user is visiting. But one module can be * loaded by loadModel() so we must get the module name of this model. * * @access public * @return string the module name. */ public function getModuleName() { $parentClass = get_parent_class($this); $selfClass = get_class($this); $className = $parentClass == 'model' ? $selfClass : $parentClass; if($className == 'extensionModel') return 'extension'; return strtolower(str_ireplace(array('ext', 'Model'), '', $className)); } /** * 设置全局超级变量。 * Set the super vars. * * @access public * @return void */ public function setSuperVars() { $this->post = $this->app->post; $this->get = $this->app->get; $this->server = $this->app->server; $this->cookie = $this->app->cookie; $this->session = $this->app->session; $this->global = $this->app->global; } /** * 加载一个模块的model。加载完成后,使用$this->$moduleName来访问这个model对象。 * 比如:loadModel('user')引入user模块的model实例对象,可以通过$this->user来访问它。 * * Load the model of one module. After loaded, can use $this->$moduleName to visit the model object. * * @param string $moduleName * @access public * @return object|bool the model object or false if model file not exists. */ public function loadModel($moduleName, $appName = '') { if(empty($moduleName)) return false; if(empty($appName)) $appName = $this->appName; $modelFile = helper::setModelFile($moduleName, $appName); if(!helper::import($modelFile)) return false; $modelClass = class_exists('ext' . $appName . $moduleName. 'model') ? 'ext' . $appName . $moduleName . 'model' : $appName . $moduleName . 'model'; if(!class_exists($modelClass)) { $modelClass = class_exists('ext' . $moduleName. 'model') ? 'ext' . $moduleName . 'model' : $moduleName . 'model'; if(!class_exists($modelClass)) $this->app->triggerError(" The model $modelClass not found", __FILE__, __LINE__, $exit = true); } $this->$moduleName = new $modelClass($appName); return $this->$moduleName; } /** * 加载model的class扩展。 * Load extension class of a model. Saved to $moduleName/ext/model/class/$extensionName.class.php. * * @param string $extensionName * @param string $moduleName * @access public * @return void */ public function loadExtension($extensionName, $moduleName = '') { if(empty($extensionName)) return false; /* Set extenson name and extension file. */ $extensionName = strtolower($extensionName); $moduleName = $moduleName ? $moduleName : $this->getModuleName(); $moduleExtPath = $this->app->getModuleExtPath($this->appName, $moduleName, 'model'); if(!empty($moduleExtPath['site']))$extensionFile = $moduleExtPath['site'] . 'class/' . $extensionName . '.class.php'; if(!isset($extensionFile) or !file_exists($extensionFile)) $extensionFile = $moduleExtPath['common'] . 'class/' . $extensionName . '.class.php'; /* Try to import parent model file auto and then import the extension file. */ if(!class_exists($moduleName . 'Model')) helper::import($this->app->getModulePath($this->appName, $moduleName) . 'model.php'); if(!helper::import($extensionFile)) return false; /* Set the extension class name. */ $extensionClass = $extensionName . ucfirst($moduleName); if(!class_exists($extensionClass)) return false; /* Create an instance of the extension class and return it. */ $extensionObject = new $extensionClass; $extensionClass = str_replace('Model', '', $extensionClass); $this->$extensionClass = $extensionObject; return $extensionObject; } /** * 加载DAO。 * Load DAO. * * @access public * @return void */ public function loadDAO() { $this->dao = $this->app->loadClass('dao'); } /** * 删除记录 * Delete one record. * * @param string $table the table name * @param string $id the id value of the record to be deleted * @access public * @return void */ public function delete($table, $id) { $this->dao->delete()->from($table)->where('id')->eq($id)->exec(); } }