Insert 2 databases at the same time, one in the cloud and the other local - CodeIgniter

2

I'm working on a project which will be handled locally, but it has to replicate to the database that is in the cloud, I'm using CodeIgniter, and I really do not know how I could do it

I leave you what I have planned to do.

This would be the configuration of the database:

$db['default'] = array(
    'dsn' => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'intranet',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

$db['nube'] = array(
    'dsn' => '',
    'hostname' => 'xxx.xxx.xxx.xxx',
    'username' => 'intranet',
    'password' => 'MY_SECRET_PASSWORD',
    'dbdriver' => 'mysqli',
    'database' => 'intranet',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

This would be my model:

function __construct() {
    parent::__construct();
    $this->db_caja = $this->load->database('caja','default', TRUE);
}

function insertar_pago() {
    $sql = $this->_obtener_data_query_pagos();
    $query = $this->db_caja->query($sql);
    return $query->result();
}
    
asked by Ivan More Flores 18.05.2017 в 19:44
source

2 answers

0

is quite easy, you just have to define an object to work each database, in the following way:

class Modelo extends CI_Model {

    // Creamos dos variables privadas, cada una para diferenciar nuestras
    // respectivas bases de datos
    private $BASE_DE_DATOS_LOCAL; // Base de datos local
    private $BASE_DE_DATOS_NUBE;  // Base de datos en la nube

    function __construct() {
        parent::__construct();

        // Inicializamos nuestro objeto de la base de datos local
        $this->BASE_DE_DATOS_LOCAL = $this->load->database('default', TRUE);
        // Inicializamos nuestro objeto de la base de datos en la nube
        $this->BASE_DE_DATOS_NUBE = $this->load->database('nube', TRUE)

    }

    function insertar_pago() {
        // Obtienes tu código SQL ...
        $sql = $this->_obtener_data_query_pagos();

        // Ejecutamos nuestro query en la base de datos local
        $query_local = $this->BASE_DE_DATOS_LOCAL->query($sql);

        // Ejecutamos nuestro query en la nube
        $query_nube = $this->BASE_DE_DATOS_NUBE->query($sql);

        // Ya depende de ti como deseas devolver el resultado ... 
        // return $query->result();
    }

}
    
answered by 18.05.2017 в 19:56
0

It's good that you create the two connection configurations, you just need to call each connection, to achieve this you could do the following:

As you have:

$db['default']
$db['nube']

As configuration arrays, to call each connection you must do it in the following way:

$DB1 = $this->load->database('default', TRUE);
$DB2 = $this->load->database('nube', TRUE);

Then to call each function of the DB you do it in the following way:

$DB1->query();
$DB1->result();

$DB2->query();
$DB2->result();

Source: link

    
answered by 18.05.2017 в 19:58