Welcome Guest, Not a member yet? Register   Sign In
Multi Database, based on user
#1

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.
Reply
#2

(This post was last modified: 01-17-2020, 12:40 PM by jreklund.)

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.
Reply
#3

(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.
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.
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
Reply
#4

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'));

// grab the most recent load date and set up the report period
$lbd = model('App\Models\ReportPeriodModel', false, $conn);
$lbd_data = $lbd->find(1);

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!
Reply
#5

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;
use CodeIgniter\Model;
use Config\Database;

class ReportPeriod extends Model
{
    protected $table = 'ReportPeriod';
    protected $primaryKey = 'id';
    protected $allowedFields = ['LoadDate', 'BuildDate'];

    public function __construct($db_group)
    {
    $this->DBGroup = $db_group;
    $this->db = Database::connect($this->DBGroup);

    $this->tempReturnType     = $this->returnType;
    $this->tempUseSoftDeletes = $this->useSoftDeletes;
    $this->tempAllowCallbacks = $this->allowCallbacks;

    $validation = \Config\Services::validation(null, false);
    $this->validation = $validation;
    }
}

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.
Reply
#6

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');
$db  = \Config\Database::connect('group_two'); 

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 )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB