包括官方網站在內的許多網站都說明了在 .a 上連接資料庫連接dbgroup,但是我幾乎找不到一個網站只討論獲取一組配置。
這個想法是我們可以制作幾個資料配接器物件,比如OAuth\Pdo沒有未使用的真實資料庫連接來獲取配置。
我怎樣才能做到這一點?
uj5u.com熱心網友回復:
通過查看系統類找到了答案CodeIgniter\Config\BaseConfig。
include 中的檔案是app/Config的app/Config/Database.php實體BaseConfig,這意味著我們可以通過以下方式獲取 Databaseconfig 的公共屬性:
$database = new \Config\Database();
通過傳遞 dbgroup 獲取配置陣列將分為三個步驟:
復制資料庫連接.env并更新值:
database.newdbgroup.hostname = localhost
database.newdbgroup.database = newdbgroup
database.newdbgroup.username = newdbgroup-username
database.newdbgroup.password = newdbgroup-password
database.newdbgroup.DBDriver = MySQLi
database.newdbgroup.DBPrefix =
在 中復制資料庫連接app/Config/Database.php。這次我們不應該更新這些值:
public $newdbgroup = [
'DSN' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
];
檢查預期結果:
$database = new \Config\Database();
echo '<pre>';
var_dump($database->newdbgroup);
echo '</pre>';
exit();
哪個輸出:
array(17) {
["DSN"]=>
string(0) ""
["hostname"]=>
string(9) "localhost"
["username"]=>
string(19) "newdbgroup-username"
["password"]=>
string(19) "newdbgroup-password"
["database"]=>
string(10) "newdbgroup"
["DBDriver"]=>
string(6) "MySQLi"
["DBPrefix"]=>
string(0) ""
["pConnect"]=>
bool(false)
["DBDebug"]=>
bool(false)
["charset"]=>
string(4) "utf8"
["DBCollat"]=>
string(15) "utf8_general_ci"
["swapPre"]=>
string(0) ""
["encrypt"]=>
bool(false)
["compress"]=>
bool(false)
["strictOn"]=>
bool(false)
["failover"]=>
array(0) {
}
["port"]=>
int(3306)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/476915.html
