Multiple Database not swapping in codeigniter 4

Viewed 57

I Have to Database group in my Database.php

public $default = [
    'DSN'      => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'primaryDB',
    'DBDriver' => 'MySQLi',
    'DBPrefix' => '',
    'pConnect' => false,
    'DBDebug'  => (ENVIRONMENT !== 'production'),
    'charset'  => 'utf8',
    'DBCollat' => 'utf8_general_ci',
    'swapPre'  => '',
    'encrypt'  => false,
    'compress' => false,
    'strictOn' => false,
    'failover' => [],
    'port'     => 3306,
];
public $second = [
    'DSN'      => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'secondaryDB',
    'DBDriver' => 'MySQLi',
    'DBPrefix' => '',
    'pConnect' => false,
    'DBDebug'  => (ENVIRONMENT !== 'production'),
    'charset'  => 'utf8',
    'DBCollat' => 'utf8_general_ci',
    'swapPre'  => '',
    'encrypt'  => false,
    'compress' => false,
    'strictOn' => false,
    'failover' => [],
    'port'     => 3306,
];

I have two type of role for login (Distributer and Vendor). in Distributer login I want to use primaryDB and for vendor login with secondaryDB.

Distributor Username - demo@distributor.com

Vendor Username - demo@vendor.com

AuthController

public function auth() {
    $username = $this->request->getPost('username');
    $password = $this->request->getPost('password');

    $database = explode('@',$username);
    $database = explode('.',$database[1])[0];

    if($database == 'distributor') {
        $userInfo = $this->AuthModel->getDistributor($username);
    } else {
        $this->db = db_connect('second');
        $userInfo = $this->AuthModel->getVendor($username);
    }

    print_r($userInfo);
}

AuthModel

public function getDistributor($username)
{
    $builder = $this->db->table('distributors');
    return $builder->get()->getRowArray();
}
public function getVendor($username)
{
    $builder = $this->db->table('vendors');
    return $builder->get()->getRowArray();
}

When I am login with my vendor account second database not loaded. it return default database values in else condition. can anyone tell me proper method to load multiple databases, because I can have more than two database for login.

1 Answers

In your AuthModel, instead of $this->db-> use db_connect('...'). I.e:


AuthModel.php

public function getDistributor($username)
{
    $builder = db_connect('default')->table('distributors');
    return $builder->get()->getRowArray();
}
public function getVendor($username)
{
    $builder = db_connect('second')->table('vendors');
    return $builder->get()->getRowArray();
}

Addendum

Solution 2: Alternatively, you could dynamically set the DBGroup on the Model instance. I.e:


AuthModel.php

// ...

public function setDB(string $databaseGroup): self {
    $this->db = db_connect($databaseGroup);
    return $this;
}

public function getDistributor($username)
{
    $builder = $this->db->table('distributors');
    return $builder->get()->getRowArray();
}
public function getVendor($username)
{
    $builder = $this->db->table('vendors');
    return $builder->get()->getRowArray();
}

// ...

AuthController.php


public function auth() {

    // ...

    $userInfo = ($database === 'distributor') ?
        $this->AuthModel->getDistributor($username)
        : $this->AuthModel->setDB("second")->getVendor($username);

    $this->AuthModel->setDB("default"); // Reset DBGroup.

    print_r($userInfo);
}

Related