Welcome Guest, Not a member yet? Register   Sign In
Loading model using a different database
#1

Hey guys, 

i am now in a situation where i have a replicated database, and in order to  be able to take the huge traffic to our system i would like to be able to redirect some select queries to a "read-only" replica of our database. 

Here is the situation: 

i have a copy of my application running on multiple instances, all with the exact same setup, all connected to the same database. 


Chancing one or more of the instances, to point to the 'read-only'  database is not an option, because some libraries (session for example) still need write access, and i also have some of my own models that need to write some stuff. 

As i researched around i stumbled upon an option, that i have never known about before: loading a model and passing an optional database connection to it. As per the documentation: 

Code:
model
($model[$name = ''[$db_conn = FALSE]])
Parameters:
  • $model (mixed) – Model name or an array containing multiple models
  • $name (string) – Optional object name to assign the model to
  • $db_conn (string) – Optional database configuration group to load
From my understanding, if i pass this third parameter to the model, all queries in this model should be routed to the database that i specify (using the default $this->db->... notation)


I tried everything (from passing a string from the predefined $db config array in database.php, passing a whole array with the needed database setup, passing it as a dsn, creating a database object and passing that) with no luck so far. 

Whenever i try to run 
PHP Code:
print($this->db->hostname); 


from within the loaded model - it always points to my default database setup, and not to the read-only setup. (also stress testing shows that those queries are going to the writer, instead of the reader database)

I would very much like to use this (assuming i have understood it correctly from the docs), as otherwise i will have to rewrite a huge part of my application, which is not even possible at this time due to organization limitations

Has anyone dealt with this? What am i doing wrong? Is there any other workaround?
Reply
#2

You need to connect to different database like below.

CodeIgniter User's Guide - Connecting to Multiple Databases
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

I know how to connect to multiple databases, also know how to load a database. 
What is unclear is that in the documentation of the Loader class it clearly states that we can pass a third parameter
  • $db_conn (string) – Optional database configuration group to load

It says it right there, that it will load (ie connect) the database configuration group provided?


Also looking at the Loader class code i see that the third parameter is indeed taken into account, but i get confused around the logic of it. 

Anyone the final result is that it is not working as i expected it to.. now i have to pass parameters to my model functions to check/load the proper database connection which is a silly workaround imo
Reply
#4

As I see it.

PHP Code:
$config['hostname'] = 'localhost';
$config['username'] = 'myusername';
$config['password'] = 'mypassword';
$config['database'] = 'mydatabase';
$config['dbdriver'] = 'mysqli';
$config['dbprefix'] = '';
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;

$this->load->model('model_name'''$config); 

You need to create the config like in the email class and then pass the config array in the third parameter.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

As i wrote in the first post - i did try that!

In the documentation the $db_conn is described as a string, so first i tried to pass in the name of my second(reader) database that i had previously configured like this: 


PHP Code:
$this->load->model('model_name''''reader');  

When that didnt work i tried providing an array (just like your example).
That didnt work too.
Last thing i tried is to get an object of the reader database and pass that in:

PHP Code:
$reader $this->load->database('reader'TRUETRUE);
$this->load->model('model_name'''$reader); 

That doesnt seem to work for me either.
Whenever i try to print $this->db->hostname from within the loaded model, i am always pointed to the initially loaded database, and not the reader one
Reply
#6

If you look at the code I posted from the User's Guide you will see that they are
creating a new config array and then passing it into the load model.

Sounds like your trying to load the ones from the ./application/config/database.php

They are stating to create a new config array like above and then pass it to the model.

Which means you would need to do it twice for each database.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB