Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any PHP Frameworks (e.g. CodeIgniter) that support database connections on a per user account basis for use in a Multi-tenant database?

I'm looking into developing a multi-tenant SaaS application, and I found several sites that describe a solid way to separate the data using tenantIDs and updateable views. e.g. This blog post

It all hinges on the ability to have your user accounts authenticated from a master users table and then having their respective database connections use those user-specific credentials. This way, the views can pull the userid and map it to the tenantID to display that user's view. However, most PHP frameworks tend to be very static when it comes to database connections (stored in text config files). They appear to be at odds.

Does anyone know: a) how to make CodeIgniter handle this gracefully? b) a different PHP framework that might?

like image 693
Brad G Avatar asked Apr 22 '10 22:04

Brad G


People also ask

How to connect db in php CodeIgniter?

In CodeIgniter, go to application/config/databse. php for database configuration file. In database. php file, fill the entries to connect CodeIgniter folder to your database.

How to connect two databases in CodeIgniter?

If you need to connect to more than one database simultaneously you can do so as follows: $DB1 = $this->load->database('group_one', TRUE); $DB2 = $this->load->database('group_two', TRUE);


2 Answers

At a horrendously basic level you can do this:

http://philsturgeon.co.uk/blog/2009/06/How-to-Multi-site-CodeIgniter-Set-up

Expand it as required, or move the logic into MY_Controller for more flexibility.

like image 91
Phil Sturgeon Avatar answered Oct 05 '22 23:10

Phil Sturgeon


There is a topic talking about this on the Code Igniter forums.

http://codeigniter.com/forums/viewthread/165227/#846845

It looks like you set up your users DB as your main database in the config file, then you generate a config array for a new connection for a user based upon information in that users DB. So, I guess you'd need to at least store the DB name in the users database.

Not sure how well this works though, as I haven't had an occasion to try it out yet.

Sorry if that's not quite what you were looking for, but it should give you an idea of a Code Igniter approach.

like image 28
Eric Allen Avatar answered Oct 05 '22 23:10

Eric Allen