Switch dynamic database from other DB in Codeigniter

前端 未结 2 1985
闹比i
闹比i 2021-01-29 04:29

mi file config.php is:

$active_group = \'default\';
$active_record = TRUE;

db master is unique DB

$db[\'master\'][\'hostname\']         


        
相关标签:
2条回答
  • 2021-01-29 04:35

    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();
        }
    
    }
    



    Check the documentation of CodeIgniter in database section:
    https://ellislab.com/codeigniter/user-guide/database/connecting.html

    Manually Connecting to a Database

    The first parameter of this function can optionally be used to specify a particular database group from your config file, or you can even submit connection values for a database that is not specified in your config file. Examples:

    To choose a specific group from your config file you can do this:

        $this->load->database('group_name');
    
    Where group_name is the name of the connection group from your config file.
    0 讨论(0)
  • 2021-01-29 04:51

    Thanks, thiagobraga. But...

    public function m_cargar_tareas_activas($group_name)
    {
        ...
    

    its parameter $group_name, would have to go for all my methods My solution is:

    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";
    

    My new file helper:

    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()
        {
            //...
        }
    

    ...And my model:

    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();
        }
    

    IMPORTANT¡ The file db_dinamic_helper.php initialize in file autoload.php Thanks..¡¡ :)

    0 讨论(0)
提交回复
热议问题