Dynamic connection in cakephp 3 -
i need dunamic connection in cakephp 3.i want change database connection per pass database name in configmanager::config. , want changed connection in controller after connetion changed.
code of appbehavior follows,
connectionmanager::drop('myconn1');
$config = connectionmanager::config('myconn1', [
'classname' => 'cake\database\connection', 'driver' => 'cake\database\driver\mysql', 'persistent' => false, 'host' => $databasedetails['res_host'], 'username' => $databasedetails['res_login'], 'password' => $databasedetails['res_password'], 'database' => $databasedetails['res_database'], 'encoding' => 'utf8', 'timezone' => 'utc', 'cachemetadata' => true,
]); connectionmanager::get('myconn1');
after above code in appbehavior have use new connection in controller.
public function changeclientconnection($databasedetails) { if(isset($databasedetails) && !empty($databasedetails)) { connectionmanager::drop('conn1'); $config = connectionmanager::config('conn1', [ 'classname' => 'cake\database\connection', 'driver' => 'cake\database\driver\mysql', 'persistent' => true, 'host' => $databasedetails['res_host'], 'username' => $databasedetails['res_login'], 'password' => $databasedetails['res_password'], 'database' => $databasedetails['res_database'], 'encoding' => 'utf8', 'timezone' => 'utc', 'cachemetadata' => false, ]); $conn = connectionmanager::get('conn1'); connectionmanager::alias('conn1', 'default'); } else { $conn = connectionmanager::get('default'); } return $conn; }
Comments
Post a Comment