用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1561|回复: 2
收起左侧

[版本 3.x] 关于模型的一个疑问

[复制链接]
发表于 2015-11-4 15:26:15 | 显示全部楼层 |阅读模式
/models 新建一个 基础模型类  base_model.php
PHP复制代码
<?php
 
class Base_model extends CI_Model {
 
 
        //数据库对象
        public $db;
 
        //数据库工具类
        public $dbutil;
 
        //数据库工厂类
        public $dbforge;
 
        public function __construct()
    {
        // Call the CI_Model constructor
        parent::__construct();
    }
       
        //连接数据库
        public function connectdb()
        {
                if ($this->db !== NULL) {
                        return;
                }
                $data = $this->auto_model->connectdb('default');
                $this->db = $data['db'];
                $this->dbutil = $data['dbutil'];
                $this->dbforge = $data['dbforge'];
        }
 
        //校验数据库
        public function checkdb()
        {
                if ( ! $this->dbutil->database_exists($this->db_name)) {
                        if ( ! $this->dbforge->create_database($this->db_name)) {
                                $this->EFUNC->RUNTIME_ERROR("create database error", $this->db_name);
                        }
                }
                $this->db->db_select($this->db_name);
        }
 
}
复制代码


想在其他的模型类继承这个类  如在/models/archives/archives_model.php
PHP复制代码
<?php
 
class Archives_model extends Base_model  {
 
        public function get_archives_id($id)
    {
                $this->connectdb();
                $this->checkdb();
 
                $query = $this->db->get_where('ht_archives',array('id' => $id));
                $row = $query->row();
                return $row->id.$row->title;
    }
 
}
复制代码

为什么继承不到这个类?   

把  Base_model   的方法直接写过来是正常的。
 楼主| 发表于 2015-11-4 15:48:46 | 显示全部楼层
ci的模型能不能继承的?
 楼主| 发表于 2015-11-4 16:40:56 | 显示全部楼层
好吧,要加个require_once APPPATH.'models/base_model.php';    才行。

本版积分规则