配置文件config.php为:
$active_group = 'default';
$active_record = TRUE;
db master是唯一的
$db['master']['hostname'] = "localhost";
$db['master']['username'] = "root";
$db['master']['password'] = "";
$db['master']['database'] = "admin_nb";
$db['master']['dbdriver'] = "mysqli";
$db['master']['dbprefix'] = '';
$db['master']['pconnect'] = FALSE;
$db['master']['db_debug'] = TRUE;
$db['master']['cache_on'] = FALSE;
$db['master']['cachedir'] = "";
$db['master']['char_set'] = "utf8";
$db['master']['dbcollat'] = "utf8_general_ci";
db app是DYNAMIC db
$db['app']['hostname'] = 'localhost';
$db['app']['username'] = 'root';
$db['app']['password'] = '';
$db['app']['database'] = $my_bd_dynamic;
$db['app']['dbdriver'] = 'mysqli';
$db['app']['dbprefix'] = '';
$db['app']['pconnect'] = TRUE;
$db['app']['db_debug'] = TRUE;
$db['app']['cache_on'] = FALSE;
$db['app']['cachedir'] = '';
$db['app']['char_set'] = 'utf8';
$db['app']['dbcollat'] = 'utf8_general_ci';
$db['app']['swap_pre'] = '';
$db['app']['autoinit'] = TRUE;
$db['app']['stricton'] = FALSE;
根据主数据库的登录,我获得了数据库的名称,并且必须将值分配给变量美元My_bd_dynamic
"bd_dynamic's"无法手动写入,因为许多数据库
我正在测试这个在我的控制器:
class Trabajador extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->driver('cache');
$this->load->helper(array('form', 'url','otros_helper','fechas_helper','imagen_helper'));
// Se le asigna a la informacion a la variable $user.
$this->abbost_hotel = @$this->session->userdata('sess_abbost_hotel_'.substr(base_url(),-8,7));
$my_bd_dynamic= $this->abbost_hotel->nombre_bd; /*var session*/
$config_app['hostname'] = 'localhost';
$config_app['username'] = 'root';
$config_app['password'] = '';
$config_app['database'] = $my_bd_dynamic;
$config_app['dbdriver'] = 'mysqli';
$config_app['dbprefix'] = '';
$config_app['pconnect'] = FALSE;
$config_app['db_debug'] = TRUE;
$this->load->model('model_tareas','',$config_app);
}
public function index()
{
//...
}
,我的Model_tarea是:…
public function m_cargar_tareas_activas()
{
$estado = 1;
$this->db->select("colum1,colum2");
$this->db->from('tarea');
$query = $this->db->get();
return $query->result_array();
}
…但是,读数据库master而不是读数据库$my_bd_dynamic。请帮助,没有办法这样操纵吗?
$this->set->database = $my_dinamic_bd;
我必须改变编码器核心的哪个部分来解决问题?
谢谢thiagobraga。但是…
public function m_cargar_tareas_activas($group_name)
{
...
它的参数$group_name,将不得不使用我的所有方法我的解决方案是:
database.php
$active_group = 'master';
$active_record = TRUE;
$db['master']['hostname'] = "localhost";
$db['master']['username'] = "root";
$db['master']['password'] = "";
$db['master']['database'] = "admin_nb";
$db['master']['dbdriver'] = "mysqli";
$db['master']['dbprefix'] = '';
$db['master']['pconnect'] = FALSE;
$db['master']['db_debug'] = TRUE;
$db['master']['cache_on'] = FALSE;
$db['master']['cachedir'] = "";
$db['master']['char_set'] = "utf8";
$db['master']['dbcollat'] = "utf8_general_ci";
我的新文件助手:
db_dinamic_helper
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
function switch_db_dinamico($name_db)
{
$config_app['hostname'] = 'localhost';
$config_app['username'] = 'root';
$config_app['password'] = '';
$config_app['database'] = $name_db;
$config_app['dbdriver'] = 'mysqli';
$config_app['dbprefix'] = '';
$config_app['pconnect'] = FALSE;
$config_app['db_debug'] = TRUE;
return $config_app;
}
trabajador.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Trabajador extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->driver('cache');
$this->load->helper(array('form', 'url','otros_helper','fechas_helper','imagen_helper','config_helper','permisos_helper'));
$this->abbost_hotel = @$this->session->userdata('sess_abbost_hotel_'.substr(base_url(),-8,7));
if(!@$this->abbost_hotel->usuario_id) redirect ('acceso');
$nombre_bd = $this->abbost_hotel->nombre_bd;
$config_app = switch_db_dinamico($nombre_bd);
$this->load->model(array('model_producto','model_trabajadores','model_habitacion','model_tareas','model_notif'));
$this->model_trabajadores->app_db = $this->load->database($config_app,TRUE);
$this->model_habitacion->app_db = $this->load->database($config_app,TRUE);
$this->model_producto->app_db = $this->load->database($config_app,TRUE);
$this->model_tareas->app_db = $this->load->database($config_app,TRUE);
$this->model_notif->app_db = $this->load->database($config_app,TRUE);
date_default_timezone_set("America/Lima");
//if(!@$this->user) redirect ('inicio/login');
//$permisos = cargar_permisos_del_usuario($this->user->idusuario);
}
public function index()
{
//...
}
…和我的模型:
model_tareas.php
<?php
class Model_tareas extends CI_Model {
public $app_db;
public function __construct()
{
parent::__construct();
}
public function m_cargar_tareas_activas()
{
$estado = 1;
$this->app_db->select("tarea_id, prioridad");
$this->app_db->from('tarea');
$query = $this->app_db->get();
return $query->result_array();
}
重要的一项在autolload .php文件中初始化文件db_dinamic_helper.php谢谢. .¡¡:)
database.php
$active_group = 'master';
$active_record = TRUE;
// SQL Server
$db['master']['hostname'] = "localhost";
$db['master']['username'] = "root";
$db['master']['password'] = "";
$db['master']['database'] = "admin_nb";
$db['master']['dbdriver'] = "mysqli";
$db['master']['dbprefix'] = '';
$db['master']['pconnect'] = FALSE;
$db['master']['db_debug'] = TRUE;
$db['master']['cache_on'] = FALSE;
$db['master']['cachedir'] = "";
$db['master']['char_set'] = "utf8";
$db['master']['dbcollat'] = "utf8_general_ci";
trabajador.php
class Trabajador extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->driver('cache');
$this->load->helper(array(
'form',
'url',
'otros_helper',
'fechas_helper',
'imagen_helper'
));
// Se le asigna a la informacion a la variable $user.
$this->abbost_hotel = @$this->session->userdata(
'sess_abbost_hotel_' . substr(base_url(), -8, 7)
);
$database_name = $this->abbost_hotel->nombre_bd; /*var session*/
$config_app = array(
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => $database_name,
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE
);
$this->load->model('model_tareas');
$result = Model_tareas::m_cargar_tareas_activas($config_app);
var_dump($result);
}
}
model_tareas.php
class Model_tareas extends CI_Model
{
public function m_cargar_tareas_activas($group_name)
{
$server = $this->load->database($group_name);
$estado = 1;
$server->select("colum1,colum2");
$server->from('tarea');
$query = $server->get();
return $query->result_array();
}
}
查看数据库部分的CodeIgniter文档:
https://ellislab.com/codeigniter/user-guide/database/connecting.html
手动连接数据库
该函数的第一个参数可用于从配置文件中指定特定的数据库组,或者您甚至可以提交配置文件中未指定的数据库的连接值。例子:
要从配置文件中选择一个特定的组,可以这样做:
<>之前$ this ->加载->数据库("group_name");之前其中group_name是配置文件中连接组的名称。