Welcome Guest, Not a member yet? Register   Sign In
Calling $this->load->database() multiple times
#4

[eluser]TheBenno[/eluser]
Thanks for the response, but I don't think you guys are understanding my problem. I tried to make my example super simple but I guess I will need to use a more complex one to illustrate:

I have a view which gets a user and gets the user's friends. Lets say that the user table is on db "USER" and the friends table is on db "FRIENDS" (on separate machines in the interest of this example). My view goes like this:

Code:
profile($uid) {
    $user = $this->User_model->get_user_by_uid($uid);
    $friends = $this->User_model->get_friends_for_uid($uid);
    //Display view with that data
}
//User Model:
get_user_by_uid($uid) {
    $user = $this->get_cached_user($uid);
    if ($user === False) {
        $sql = 'SELECT * FROM user WHERE uid = ?';
        $udb = $this->load->database('user', TRUE);
        $user = $udb->query($sql, array($uid))->row_array();
    }
    return $user;
}

get_friends_for_uid($uid) {
    $friends = $this->get_cached_friends($uid);
    if ($friends === False) {
        $sql = 'SELECT * FROM friends WHERE uid = ?';
        $fdb = $this->load->database('friends', TRUE);
        $friends = $fdb->query($sql, array($uid))->result_array();
    }
    return $friends;
}

So basically I DON'T want to connect to any databases unnecessarily (see MDB2 and it's singleton() method for a great example of how this can be done) and more often then not i will NOT need to connect to any databases. I want to connect to my databases lazily. Is there some way that I wrap "load->database()" to check to see if an existing connection exists so that every call to load->database returns the singleton instance? I could build this functionality into my models, but potentially I could connect to the same database in multiple models, so I really need the connection cacheing/singleton statically throughout the runtime of the script.

Thanks


Messages In This Thread
Calling $this->load->database() multiple times - by El Forum - 06-07-2010, 12:56 PM
Calling $this->load->database() multiple times - by El Forum - 06-07-2010, 01:30 PM
Calling $this->load->database() multiple times - by El Forum - 06-07-2010, 01:33 PM
Calling $this->load->database() multiple times - by El Forum - 06-07-2010, 03:08 PM
Calling $this->load->database() multiple times - by El Forum - 06-08-2010, 01:08 AM
Calling $this->load->database() multiple times - by El Forum - 06-08-2010, 01:24 AM
Calling $this->load->database() multiple times - by El Forum - 06-08-2010, 06:45 AM
Calling $this->load->database() multiple times - by El Forum - 06-23-2010, 03:04 AM
Calling $this->load->database() multiple times - by El Forum - 06-23-2010, 03:35 AM
Calling $this->load->database() multiple times - by El Forum - 06-23-2010, 03:59 AM
Calling $this->load->database() multiple times - by El Forum - 01-12-2011, 04:13 PM
Calling $this->load->database() multiple times - by El Forum - 01-12-2011, 04:32 PM



Theme © iAndrew 2016 - Forum software by © MyBB