Over the last few months I have been using the CodeIgniter framework for several projects. So far, I have been very pleased with its flexibility and even more so with its extensibility.
On one my projects, I needed to connect to multiple databases at the same time. CI easily supports this via configuration settings, however the built in profiler only supports the default database. No problem, let’s extend it!
Step 1: Edit database.php and use descriptive group names.
In this tutorial, I am connecting to 3 databases. So far, this is nothing new, however your settings need to be correct.
Example:
... $active_group = "db1"; ... $db['db1']['database'] = "database1"; ... $db['db2']['database'] = "database2"; ... $db['db3']['database'] = "database3"; ...
Step 2: Edit autoload.php and autoload ALL models.
This is needed so the profiler is aware of the other databases and can iterate accordingly.
IMPORTANT: If you ONLY load your models on demand, that’s OK too. You still want to follow this step, but simply comment out the $autoload variable when you are NOT in debug mode.
Example:
... $autoload['model'] = array ( 'table1', 'table2', 'table3' ); ...
Step 3: In your models, use the actual database group names for your db connection.
In other words, stop using $this->db and start using $this->[group], etc.
IMPORTANT: Be sure to establish a connection to the database group in the model’s constructor AND add a new method to get the database group for this mode. THIS IS REQUIRED.
Example:
class Table1 extends Model
{
// Don't forget to connect this group in the constructor
function Table1()
{
...
$this->db1 = $this->load->database('db1', TRUE);
...
}
// Required method to get the database group for THIS model
function get_database_group() {
return 'db1';
}
...
}
Now in EACH method in THIS model, use $this->db1.
Example:
class Db1 extends Model
{
...
function get_all_records()
{
$this->db1->from('table1');
$query = $this->db1->get();
...
return $query->result_array();
}
...
}
Step 4: Extend CI’s profiler class to include all databases in the debug output
In your config.php file, take note of your subclass_prefix setting. You will need to use this in order to extend CI’s core classes automatically.
Example:
... $config['subclass_prefix'] = 'MY_'; ...
Now, create a new file in your application’s libraries directory called MY_Profiler.php.
IMPORTANT: If you changed your subclass_prefix, replace “MY_” in the rest of this tutorial with your custom setting.
In this new file, you will be extending the CI_Profiler, defining your own run method to account for multiple database groups, and adding a new display method that shows the database group AND model for each query!
Example:
...
class MY_Profiler extends CI_Profiler {
/**
* Compile Multiple Database Queries
* @return string
*/
function _compile_multi_db_queries($database, $model)
{
... too much to copy here ... download file to review ...
}
/**
* Run the Profiler
* @return string
*/
function run()
{
...
// Include the autoload config to access the array of models in this app.
include(APPPATH.'config/autoload'.EXT);
// Loop through each model to set the database object
foreach($autoload['model'] as $model) {
// Define the database object name
$database = $this->CI->$model->get_database_group();
// Compile the output
$output .= $this->_compile_multi_db_queries($database, $model);
}
...
}
}
The following file should do the trick. Simply the MY_Profiler.php file to your application’s libraries directory and enjoy! And don’t forget to change the “MY_” prefix if needed.
UPDATE: Codeigniter Profiler Extended Download
There may be cleaner ways to accomplish this same task, but once setup and used consistently, it works as great. Comments & feedback welcome.
No related posts.
Great article! Looking for these for weeks.
Thanks a lot buddy you’re a genius!!
Just read user guide this is all there
This is just collection of those. Only one good thing …this guys summarize all this.
Like your post.
Might need it in the near future.
Nice job Michael! Really useful for my project. Thank u!
This looks like one good code. This will surely help a lot of coders out there.
Hi Michael,
very helpful post, thank you.
Thank you for this nice tip
Nice and neat! Just a note that MY_Profiler.zip need to be updated though – or one could just need to copy and past the run() from current version adn modify.
Great job Michael!
Thanks for all of the comments guys. I have updated the MY_Profiler.php file with some new functionality. Check it out here: http://www.gotphp.com/php/codeigniter-profiler-extended/54358/
Enjoy!