今天要做一个model同样的分级,功能实现了,做个笔记记录
和model层同级的创建文件夹table,如下图
1.在system/core下创建Table.php文件,代码如下:
class CI_Table { /** * Constructor * * @access public */ function __construct() { log_message('debug', "Table Class Initialized"); } /** * __get * * Allows models to access CI's loaded classes using the same * syntax as controllers. * * @param string * @access private */ function __get($key) { $CI =& get_instance(); return $CI->$key; } }2.打开为文件system/core/Loader.php文件
添加如下代码
/** * List of paths to load tables from * * @var array * @access protected */ protected $_ci_tables_paths = array(); /** * List of loaded tables * * @var array * @access protected */ protected $_ci_tables = array();
修改方法
public function __construct()里添加$this->_ci_tables_paths = array(APPPATH);
public function initialize()里添加$this->_ci_tables = array();
private function _ci_autoloader()里添加// Autoload tables if (isset($autoload['table'])) { $this->table($autoload['table']); }
最后再添加table方法(copy model 方法)
public function table($table, $name = '', $db_conn = FALSE) { if (is_array($table)) { foreach ($table as $babe) { $this->table($babe); } return; } if ($table == '') { return; } $path = ''; // Is the model in a sub-folder? If so, parse out the filename and path. if (($last_slash = strrpos($table, '/')) !== FALSE) { // The path is in front of the last slash $path = substr($table, 0, $last_slash + 1); // And the model name behind it $table = substr($table, $last_slash + 1); } if ($name == '') { $name = $table; } if (in_array($name, $this->_ci_tables, TRUE)) { return; } $CI =& get_instance(); if (isset($CI->$name)) { show_error('The model name you are loading is the name of a resource that is already being used: '.$name); } $model = strtolower($table); foreach ($this->_ci_model_paths as $mod_path) { if ( ! file_exists($mod_path.'tables/'.$path.$table.'.php')) { continue; } if ($db_conn !== FALSE AND ! class_exists('CI_DB')) { if ($db_conn === TRUE) { $db_conn = ''; } $CI->load->database($db_conn, FALSE, TRUE); } if ( ! class_exists('CI_Table')) { load_class('Table', 'core'); } require_once($mod_path.'tables/'.$path.$table.'.php'); $table = ucfirst($table); $CI->$name = new $table(); $this->_ci_tables[] = $name; return; } // couldn't find the table show_error('Unable to locate the table you have specified: '.$table); }3.在新建目录tables里添加类test_table.php
代码如下:
class test_Table extends CI_Table { public function __construct() { } /** * test */ function test() { echo 'Tables test is ok'; } }
4.在controllers新加文件tests.php
代码如下:
class tests extends CI_Controller { function __construct() { parent::__construct(); } function index() { $this->load->table('test_table'); $this->test_table->test(); } }这就完成!