CodeIgniter Forums

Full Version: Database Switching to Test Environment.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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?

El Forum

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

El Forum

[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?

El Forum

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

El Forum

[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-gui...iling.html

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