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

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: 

($model[$name = ''[$db_conn = FALSE]])
  • $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:

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?

Messages In This Thread
Loading model using a different database - by korgoth - 08-04-2018, 02:06 AM

Theme © iAndrew 2016 - Forum software by © MyBB