sjmhai 发表于 2015-11-4 15:26:15

关于模型的一个疑问

/models 新建一个 基础模型类base_model.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

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   的方法直接写过来是正常的。

sjmhai 发表于 2015-11-4 15:48:46

ci的模型能不能继承的?

sjmhai 发表于 2015-11-4 16:40:56

好吧,要加个require_once APPPATH.'models/base_model.php';    才行。
页: [1]
查看完整版本: 关于模型的一个疑问