Reputation: 49833
I'm using 2 different databases on my CI app.
I created 2 users on mysql, and granted user1 to db1 all privileges, then user2 to db2 all privileges with SQLpro client.
The database.php:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '***1';
$db['default']['password'] = '***1';
$db['default']['database'] = 'ci_users';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_unicode_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['keys']['hostname'] = 'localhost';
$db['keys']['username'] = '***2';
$db['keys']['password'] = '***2';
$db['keys']['database'] = 'ci_keys';
$db['keys']['dbdriver'] = 'mysql';
$db['keys']['dbprefix'] = '';
$db['keys']['pconnect'] = TRUE;
$db['keys']['db_debug'] = TRUE;
$db['keys']['cache_on'] = FALSE;
$db['keys']['cachedir'] = '';
$db['keys']['char_set'] = 'utf8';
$db['keys']['dbcollat'] = 'utf8_unicode_ci';
$db['keys']['swap_pre'] = '';
$db['keys']['autoinit'] = TRUE;
$db['keys']['stricton'] = FALSE;
The models:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_Users extends CI_Model {
function Model_Users()
{
// Call the Model constructor
parent::__construct();
$this->load->database('default',true);
}
etc...
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_Keys extends CI_Model {
function Model_Keys()
{
// Call the Model constructor
parent::__construct();
$this->load->database('keys',true);
}
etc...
So when using both models in same controller/method the model_users works great but model_keys returns an error, because it seems to load anyway the model_users db1 instead ofl oading db2:
Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/app/application/models/model_keys.php:2) Filename: core/Common.php
Line Number: 442A Database Error Occurred
Error Number: 1146
Table 'ci_users.ci_keys' doesn't exist //this should be ci_keys.ci_keys
Does anyone has idea?
I edited code in model_keys:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_Keys extends CI_Model {
function Model_Keys()
{
// Call the Model constructor
parent::__construct();
$this->db_keys = $this->load->database('keys',true);
}
Now db works but I receive session error on model:
Message: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/site/application/models/model_keys.php:2)
In controller i'm doing this:
$this->load->model('model_users');
$this->model_users->insert(bla bla);
$this->load->model('model_keys');
$this->model_keys->insert(bla bla);
$this->session->set_userdata(array(bl bla);
redirect(some url);
Upvotes: 1
Views: 889
Reputation: 15981
Below line is problem
$this->load->database('keys',true);
If you pass second argument TRUE, then it will return database object and it will not work with ActiveRecord.
Here is example
$DB1 = $this->load->database('group_one', TRUE);
//Then you can't below function
$this->db->query();
You will instead use:
$DB1->query();
Upvotes: 3