0

mi file config.php is:

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

db master is unique DB

$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 is 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;

According to a login in the master database, I get the name of the database and I have to assign the value to the variable $ My_bd_dynamic

The "bd_dynamic's" can not write it manually because many databases

I was testing this in my controller:

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

And, my Model_tarea is: ...

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

... But, read database master and not read database $my_bd_dynamic. Help Please, There is no way to manipulate this way?

$this->set->database = $my_dinamic_bd;

What part of the core of codeigniter I have to change to fix the problem?

2 Answers 2

7

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..¡¡ :)

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.