Welcome Guest, Not a member yet? Register   Sign In
Yet another multi database issue..
#1

[eluser]auximage[/eluser]
Searched through and saw basics of what's going on but this seems like a 'special' issue to me..

I have setup my db config file to have multiple groups, and assigned an active group to the db that I am connecting to primarily (via FreeTDS). Connections to this db work fine. However, in a second model I don't need access to that db at all, so I'm attempting to connect to the second group setup in the config. Ideally it would look like this:

Code:
class Phone_Systems extends Model {
function __construct() {
        parent::__construct();
        $this->load->database('phone_systems');  //load the alternate db group
}

function get_logins() {
  $sql = "select * from logins";
  $query = $this->db->query($sql);
  //Loop row, output data to test pulling data out of dB



}

}

However, when calling this I get the following error..
Quote:A Database Error Occurred

Error Number: S0002

[unixODBC][FreeTDS][SQL Server]Invalid object name 'logins'.

Looks like it's trying to use the 'active' group connection even through I thought my construct was overriding that. OK.. guess I'm doing it wrong.. Lets try it in the way suggested in the manual..


Code:
class Phone_Systems extends Model {
function __construct() {
        parent::__construct();
        $this->alt_db = $this->load->database('phone_systems', TRUE);  //load the alternate db group
}

function get_logins() {
  $sql = "select * from logins";
  $query = $this->alt_db->query($sql);
  //Loop row, output data to test pulling data out of dB



}

}


Works.. how about that. . I guess my question is, why does it not make the association for $this->db (and assign it to the alternate database) to work when I'm calling that in the constructor? Am I just missing something? Should I not be trying to load the database object in the constructor? Since this method is ONLY going to be accessing data from this alternate database, what's the cleanest way to make $this->db->xx calls work without having to use the $this->alt_db->xx way?

Sorry if this it a totally obvious question, I'm new to CI, but obviously no PHP superstar to see why this isn't working off the bat.
#2

[eluser]danmontgomery[/eluser]
It's already connected to one DB, you can't use $this->load->database() to close one connection and establish another on the same $this->db object (Generally speaking, when you call $this->load, the loader checks to see if the object exists already, to prevent loading resources multiple times).

So, you shouldn't have a problem if you just don't load the first database initially, otherwise you would have to use the $this->alt_db method.




Theme © iAndrew 2016 - Forum software by © MyBB