|
自定义MY_Model,构造方法里连接默认数据库
class MY_Model extends CI_Model
{
//数据库连接
public $db = '';
//调用数据库的配置项
public $db_setting = 'default';
//数据表名
public $table_name = '';
public function __construct()
{
parent::__construct();
$this->db = $this->load->database($this->db_setting, true);
}
//封装了常用的增删改查,其他方法省略
public function insert($data, $return_insert_id = false, $replace = false)
{
if(!is_array( $data ) || count($data) == 0) return false;
$return = $replace ? $this->db->replace($this->table_name, $data) : $this->db->insert($this->table_name, $data);
return $return_insert_id ? $this->db->insert_id() : $return;
}
//有个model需要连接另外一个库
class tel_doctor_mdl extends MY_Model
{
private $per_db;
public function __construct()
{
$this->table_name = 'tel_doctor';
parent::__construct();//连接默认库 tel
//连接另外一个库
$this->per_db = $this->load->database('person', true);//personcenter
在自定义controller的构造方法里设置了session保存到数据库(load session_mysql)
libraries/session_mysql.php
public function __construct()
{
$this->ci = &get_instance();
$this->ci->load->model('admin/session_mdl');
$this->lifetime = $this->ci->config->item('session_ttl'); session_set_save_handler(array(&$this,'open'), array(&$this,'close'), array(&$this,'read'), array(&$this,'write'), array(&$this,'destroy'), array(&$this,'gc'));
session_start();
}
public function write($id, $data)
{
$uid = isset($_SESSION['userid']) ? $_SESSION['userid'] : 0;
$roleid = isset($_SESSION['roleid']) ? $_SESSION['roleid'] : 0;
$c = defined('ROUTE_C') ? ROUTE_C : '';
$a = defined('ROUTE_A') ? ROUTE_A : '';
if(strlen($data) > 255) $data = '';
$ip = ip();
$sessiondata = array(
'sessionid'=>$id,
'userid'=>$uid,
'ip'=>$ip,
'lastvisit'=>SYS_TIME,
'roleid'=>$roleid,
'c'=>$c,
'a'=>$a,
'data'=>$data,
);
return $this->ci->session_mdl->insert($sessiondata, true, true);
}
//model都继承MY_Model
class session_mdl extends MY_Model
{
public function __construct()
{
$this->table_name = 'session';
parent::__construct();
}
}
A Database Error Occurred
Error Number: 1146
Table 'personcenter.ci_session' doesn't exist
REPLACE INTO `ci_session` (`sessionid`, `userid`, `ip`, `lastvisit`, `roleid`, `c`, `a`, `data`) VALUES ('575ce85019995c839b87c11655bfb941', '1', '127.0.0.1', 1318906430, '1', 'tel_doctors', 'details', 'login_enter|s:3:\"lhl\";code|s:4:\"fyg9\";userid|s:1:\"1\";roleid|s:1:\"1\";lock_screen|i:0;')
Filename: E:\php\xx.xx.com\system\database\DB_driver.php
Line Number: 330
怎么数据库就用了personcenter ?
按照流程来说 是 到一个一个控制xx中,内容调用的 load->model('tel_doctor_mdl') session 应该是在这个之前已经写了,load了这个model 又要写session ? |
|