Welcome Guest, Not a member yet? Register   Sign In
Multiple Databases Best Practice
#1

[eluser]matthewr[/eluser]
Hi! What's the best practice for connecting to multiple databases and using Active Record to access them both. I'll have 1 primary database that my app will access a lot, and one secondary database that my app will check out probably only during logins. I need two databases because the second database will be accessed by multiple apps. Could someone show me the best practice for doing this? I just want to do it right.

Thanks
Matthew
#2

[eluser]ajrowland[/eluser]
Hello. The user guide has a section on this here.

Look at Connecting to Multiple Databases, near the bottom of the page.

Does this help?
#3

[eluser]matthewr[/eluser]
Hi Ajrowland,

Thanks, but I've already read that part but it doesn't really show practical examples on how best to use them. I'm worried that i might develop my entire app on my guess of how best to code that only to find out that it's not a good standard. Is there any article out there or sample code that makes use of multiple DBs?

Matt
#4

[eluser]matthewr[/eluser]
Don't we have any open source projects out there that connect to multiple databases?
#5

[eluser]ajrowland[/eluser]
I've not come across what you would call best practice regarding this, so I can only (humbly) offer advice on what I would do.

I think your main concern would be where you would declare your second connection. This consideration would be the same as for any reusable code. If you are only checking credentials for a user, maybe in the method itself, ie:

Code:
<?php

class Article extends Controller {

    function Article()
    {
        parent::Controller();
    }

    function index()
    {
        $this->_check_user();

        $data['articles'] = $this->db->get('articles');

        $this->load->view('article_view', $data);
    }

    function _check_user()
    {
        $db_auth = $this->load->database('auth', TRUE);

        // Check session data against authentication database etc,
        // using $db_auth object.
    }
}
?>

Or maybe in the constructor, if you want to reuse the connection across methods:

Code:
<?php

class Article extends Controller {

    function Article()
    {
        parent::Controller();

        $this->db_auth = $this->load->database('auth', TRUE);
    }

    function index()
    {
        $this->_check_user();

        $data['articles'] = $this->db->get('articles');

        $this->load->view('article_view', $data);
    }

    function _check_user()
    {
        // Check session data against authentication database etc,
        // using $this->db_auth object.
    }
}
?>

Or go one further and extend the CodeIgniter controller to make the connection available to all controllers. Or use a library etc. As I say i'm not sure this comes down to best practice, but how often, and where you use the connection. i.e., a general application design consideration.

Note, I've accessed the main database using the standard method ($this->db), and used an object reference for the 'auth' connection group in my database.php config. I think this is something I would generally do.

I was having trouble using multiple connections until a looked here. You can only have one persistant connection in your config. Which is why it look so long to reply!

And finally, maybe reflect whether you really need multiple databases? Sorry, had to throw that one in.

I'm sure you've considered all this, but I hope this may confirm or dispell any ideas you may have.

Andy




Theme © iAndrew 2016 - Forum software by © MyBB