CodeIgniter Forums
Database Switching to Test Environment. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Database Switching to Test Environment. (/showthread.php?tid=20591)



Database Switching to Test Environment. - El Forum - 07-15-2009

[eluser]heha[/eluser]
I have database config file like this:

Code:
$active_group = "development";
$active_record = TRUE;

/************* Development Environment **************/
$db['development']['hostname'] = "localhost";
$db['development']['username'] = "root";
$db['development']['database'] = "project_development";
...
...

/************* Test Environment **************/
$db['test']['hostname'] = "localhost";
$db['test']['username'] = "root";
$db['test']['database'] = "project_test";
...
...

Then I have a Controller "Unit_test" to do all test specific. I have add following code to make a database switching:

Code:
// use Test database environment
    unset($this->db);
    $this->load->database('test');
    $this->db->get('user');
    $this->load->model('user');
    $this->user->get_by_id(1);
    echo $this->db->last_query();

It looks OK except for one thing, last_query() function. It print "SELECT * FROM user" instead of "SELECT * FROM user JOIN user_type ON user.id = user_type.user_id" which is in "User_model" class. I must copy and paste "echo $this->db->last_query();" to inside function "get_by_id" to get the correct result. It looks like "$this->db" that I called in controller wasn't the same "$this->db" that I called in model anymore. Maybe it cause some problem because "unset($this->db);" line but I cannot omit this line out since it won't load the test database config to override development database config.

What should I do?


Database Switching to Test Environment. - El Forum - 07-15-2009

[eluser]Evil Wizard[/eluser]
try this
Code:
echo $this->user->db->last_query();
to see if that brings back the right sql


Database Switching to Test Environment. - El Forum - 07-15-2009

[eluser]heha[/eluser]
[quote author="Evil Wizard" date="1247672786"]try this
Code:
echo $this->user->db->last_query();
to see if that brings back the right sql[/quote]

Yes, it brought me the right sql. Then what should I do? Do I need to call "$this->user->db->last_query()" in all line that I want?


Database Switching to Test Environment. - El Forum - 07-15-2009

[eluser]Evil Wizard[/eluser]
It means that the instance of the db is not the same as the instance in the user model


Database Switching to Test Environment. - El Forum - 07-15-2009

[eluser]Maglok[/eluser]
You could load the profiler, that should print all the used queries in generating a page. It has issues on PHP 4 though.

http://ellislab.com/codeigniter/user-guide/general/profiling.html

Code:
$this->output->enable_profiler(TRUE);