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.