Multi Database, based on user |
I have searched quite a bit on this subject, and seem to keep falling short on how to make it work for my situation.
Background. I have system in mind where a user logs in, and there database is loaded with their content. My current design is thus: Database 1 = Core, with tables for users, access privileges and database configurations. Each database after that is specific to a company that has their specific data. After a user logins, their user info and database connection info is stored in the session. So user A, logins in and their Company A database is loaded. user b login in and Company B database is loaded. I have tried a number of ways and keep failing. I may be going about this the wrong way. Any suggestions or ideas would be greatly appeciated.
You need to create multiple database configs in your /application/config/databse.php and load the default (at login) and specifically load their own database in all other files.
https://codeigniter.com/user_guide/datab...a-database If you have done this; You need to show us some code and examples where it's not working and specify what you problems are. (01-17-2020, 12:39 PM)jreklund Wrote: You need to create multiple database configs in your /application/config/databse.php and load the default (at login) and specifically load their own database in all other files.thank you for your help. Did some more searching and figured out how and where to load the individual databases. My apologizes for not including code. I wasn't sure what to show or where to start. Thank you
I am doing just as mentioned here and I am still not able to connect to another database. I'm using SQLite3 with CodeIgniter 4.0.4. My default connection is a different database file to validate logins and such. Trying to switch to another db after validating login to allow for different users (customers) to access their own data. Here is the relevant code:
Code: $conn = db_connect($this->session->get('login_database')); I keep getting a "SQLite3::query(): Unable to prepare statement: 1, no such table: ReportPeriod" error. The value of the "login_database" session var points to a different config variable in the database.php config file (as noted). The database property on this variable points to an SQLite3 file in the write path which does indeed have a table called "ReportPeriod" in it. Not sure how to resolve this. Any help would be much appreciated!
I did find a workaround for this (I doubt its a bug, probably just something I don't understand). I was able to resolve it by extending the constructor into my model like so:
Code: <?php namespace App\Models; So I pass in the group name from the Database.php config file based on other criteria. This way I am able to set the default connection on the model to a different database as applicable. Hope this may help someone.
Connecting to Multiple Databases
If you need to connect to more than one database simultaneously you can do so as follows: PHP Code: $db1 = \Config\Database::connect('group_one'); Note: Change the words “group_one” and “group_two” to the specific group names you are connecting to. Group_one and Group_two would be in the Config/Database.php You define a new group besides the default group. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |