CodeIgniter Forums

Full Version: How to reuse an established DB connection in an another controller ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]FabD[/eluser]
Hi,

I'm a newbie in the use of MVC frameworks. I have choosen CodeIgniter for many known reasons.
From now on, I enjoy working/playing with it. But now serious things are coming and real questions too. I have a question wich may seems basic to many of you :

I have a 'main' Controller wich via a Login Model establish a connection to an Oracle DB, after few basic controls about the user.
How can I reuse the connection establihed in the main controller in an another controller ?
Must I redo a DB connection into that controller too ? Have I missed something ?

Thank for helping me,
Fab

El Forum

[eluser]Aniket[/eluser]
Not at all required. If you autload the database by editing application/config/autoload.php you dont have to keep on using
Code:
$this->load->database();
repeatedly in each controller. CI does it for you.
You just have to edit the autoload array to
Code:
$autoload['libraries'] = array('database');

El Forum

[eluser]FabD[/eluser]
Aniket,
Thank for your answer. I see what you mean but the autoload is Ok if you know the user/password but here the user has to log in the application and based on what the user entered as info I do this in my main controller :

Code:
$dsn = 'oci8://'.$user.':'.$password.'@local_db';        
$this->load->database($dsn);

$user and $password are not already known while the autoload runs.

Do I have to repeat that operation in every controller ?

El Forum

[eluser]TheFuzzy0ne[/eluser]
Where does the username and password come from?

El Forum

[eluser]FabD[/eluser]
the user/password come from a login view with a form.

El Forum

[eluser]TheFuzzy0ne[/eluser]
OK, so your user will need to be logged in, and will therefore have cookies enabled. You could store the username and password in the session, and create a custom controller that will load the database. Something like this:

./system/application/libraries/MY_Controller.php
Code:
class MY_Controller extends Controller {
    
    function MY_Controller()
    {
        parent::Controller();

        if ($db_info = this->session->userdata('db_data');)
        {
            // The users information can be stored in an array in the session.
            $dsn = 'oci8://' . $db_info['username'] . ':' . $db_info['password'] . '@local_db';        
            $this->load->database($dsn);
        }
    }
}

// End of file: MY_Controller.php
// Location: ./system/application/libraries/MY_Controller.php
The above code is untested.

Just ensure any controllers that require your user to be logged in extend the MY_Controller class.

You might need some more logic in there to handle situations where the information is incorrect before the database is called upon by any of your controller methods, but hopefully this will help you.

El Forum

[eluser]FabD[/eluser]
Thanks,

Yes indeed, that's the kind of thing I was looking for.

I have seen that some people load manually the database into a Model and others into a Controller.
What is the best practice ?

El Forum

[eluser]TheFuzzy0ne[/eluser]
It all depends. I wouldn't say there's a best practice. It all depends on where you need to database. If you don't user it in your controller, and do in your model, then the model would probably be the better option, if you do use it in your controller, load it there. It's really up to you, so do what makes sense to you.