Welcome Guest, Not a member yet? Register   Sign In
Use the same model with two different connections
#1

[eluser]rafabayona[/eluser]
Hi, I want to use a model with the 'default' database connection, but in some controllers, use the same model with other connection. I've just discovered the third parameter while loading models http://ellislab.com/codeigniter/user-gui....html#conn, but I can't make it work; it still uses the default connection and ignores the config array I pass as the third parameter.

Maybe the problem is autoloading the database class with the 'default' database?
#2

[eluser]rogierb[/eluser]
What I would do in these models is close the database and depending on the third parameter connect to the database you want.

I think you are correct as to assume the autoloading doesnt play nice
#3

[eluser]rafabayona[/eluser]
Thanks for answering.

I'm using $this->db->close() before loading the model but it doesn't seem useful because of this: http://ellislab.com/forums/viewthread/186952/#883735

In that thread, a user suggests "overwriting" $this->db with the new connection, but I don't know if it's advisable.
#4

[eluser]Marcky[/eluser]
Hi Rafa,

This is how I managed to get it working:

Code:
<?php

class Usuarios_model extends CI_Model {

private $db;
private $mdb;

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

public function validar()
{
  $this->db->where('email', $this->input->post('email'));
  $this->db->where('password', md5($this->input->post('password')));
  $query = $this->db->get('usuarios');
  
  if($query->num_rows() == 1)
  {
   //usuario vĂ¡lido
   return true;
  }
}

        ...

And in config/database.php:

Code:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'xxxxxxx';
$db['default']['password'] = 'xxxxxxx';
$db['default']['database'] = 'xxxxxxx';
$db['default']['dbdriver'] = 'mysql';
...

$db['mdb']['hostname'] = 'localhost';
$db['mdb']['username'] = 'xxxxxxx';
$db['mdb']['password'] = 'xxxxxxx';
$db['mdb']['database'] = 'xxxxxxx';
$db['mdb']['dbdriver'] = 'pdo';
...

Hope it helps!
#5

[eluser]rafabayona[/eluser]
Thanks Marcky Wink but how do you switch between the connections?

You've given me an idea. Would be possible doing this outside the model, in the controller...

Code:
$this->db2 = $this->db = $this->load->database('c1112', TRUE);

And then use the $this->db2 in the models? This way I could switch between connections using the same model...
#6

[eluser]Marcky[/eluser]
Hi again Rafa,

why would you want to "switch" between connections? I'm not sure I'm understanding your question... so you have the same table in two different db's?

You may store each reference to databases in separate class properties, I did so for mysql ($db) and access ($mdb):

Code:
private $db;
private $mdb;

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

So whenever I want to run a query against mysql, I use $this->db->query(), and when involving msaccess, I'm using $this-mdb->query().

Maybe I'm breaking here the principle of "1 table - 1 model" mentioned in the user guide, but I find this approach easier to maintain since I'm performing actions in my two databases with things related to users. My real situation is I've built a login system, storing login credentials into mysql, but at the same time I'm inserting extra user data into the mdb file, so I want to perform both actions in the same model. As said, I might be breaking CI's MVC philosophy with this, but I find it a lot easier. In the end, I'm not doing extra work inside the model, just database stuff.

So what I asked you at the beginning, I you have the same table inside both databases, you might want to build query functions that may work whatever is your connection (big thanks Active Record!) and run them from $this->db, and inside the constructor assign one connection (or the other) to $this->db depending on your circumstances.
#7

[eluser]rafabayona[/eluser]
The organisation I'm working for stores data isolated by year; each year has its own database with the same tables, fields, etc. That's why I want to reuse the models. But I need to switch between connections at will in order to get data from older years.

I know it's weird, but...




Theme © iAndrew 2016 - Forum software by © MyBB