CI框架下 创建自己的model层

今天要做一个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();
    }
}
这就完成!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值