Add setInstance() support for runtime database switching
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 35/100
- Issue 類型
- 功能
- 描述清晰度
- 基本清楚
- 活躍度
- 停滯
- 技術堆疊
- mysql, php
研究方向
從 system/Database/Config.php 開始,閱讀現有的 Config::connect() 流程,包括 ensureFactory()、factory 載入呼叫以及執行個體儲存。將提議的 setInstance() 行為與該流程進行比較,並確定自訂組態以及後續 connect('tenant') 呼叫的驗收情況。
由索引模型根據 Issue 內容生成。
描述
PHP Version
8.3
CodeIgniter4 Version
4.6.1
CodeIgniter4 Installation Method
Composer (using codeigniter4/appstarter)
Which operating systems have you tested for this bug?
Windows
Which server did you use?
apache
Database
MySQL
What happened?
I would like to request a new method in Config\Database that allows developers to set or override a database connection instance at runtime without modifying the framework core.
This is especially useful when a project needs dynamic database switching, such as in:
Multi-tenant SaaS applications
ERP systems with client-specific databases
“Switch DB per request” API requirements
Dynamic “connect with custom credentials” scenarios
Currently, CodeIgniter 4 allows Database::connect($group) but there is no official method to replace or inject a database instance for a given group.
Proposed Method
Add this method in system/Database/Config.php:
public static function setInstance(string $group, array $config)
{
static::ensureFactory();
$connection = static::$factory->load($config, $group);
static::$instances[$group] = $connection;
return $connection;
}
Why this is needed
Today, Database::connect() always loads config from app/Config/Database.php.
To use a custom connection (hostname, username, password, database), we must create a fake group name or override config objects — both are hacky.
Dynamic DB switching is common in modern SaaS, ERP, and enterprise applications.
This addition would provide a clean, supported API for runtime DB instance control.
Example Use Case
$dbConfig = [
'DSN' => '',
'hostname' => $tenantHost,
'username' => $tenantUser,
'password' => $tenantPass,
'database' => $tenantDb,
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
];
$connection = \Config\Database::setInstance('tenant', $dbConfig);
$db = \Config\Database::connect('tenant');
This gives developers the power to switch databases safely and officially.
Backward Compatibility
This method does not change any existing behavior.
It only adds a new optional utility method.
No BC breaks.
Conclusion
This feature will make CodeIgniter 4 significantly more flexible for advanced and enterprise applications that require dynamic DB switching.
Thank you for considering this enhancement! 🙏
Steps to Reproduce
I would like to request a new method in Config\Database that allows developers to set or override a database connection instance at runtime without modifying the framework core.
This is especially useful when a project needs dynamic database switching, such as in:
Multi-tenant SaaS applications
ERP systems with client-specific databases
“Switch DB per request” API requirements
Dynamic “connect with custom credentials” scenarios
Currently, CodeIgniter 4 allows Database::connect($group) but there is no official method to replace or inject a database instance for a given group.
Proposed Method
Add this method in system/Database/Config.php:
public static function setInstance(string $group, array $config)
{
static::ensureFactory();
$connection = static::$factory->load($config, $group);
static::$instances[$group] = $connection;
return $connection;
}
Why this is needed
Today, Database::connect() always loads config from app/Config/Database.php.
To use a custom connection (hostname, username, password, database), we must create a fake group name or override config objects — both are hacky.
Dynamic DB switching is common in modern SaaS, ERP, and enterprise applications.
This addition would provide a clean, supported API for runtime DB instance control.
Example Use Case
$dbConfig = [
'DSN' => '',
'hostname' => $tenantHost,
'username' => $tenantUser,
'password' => $tenantPass,
'database' => $tenantDb,
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
];
$connection = \Config\Database::setInstance('tenant', $dbConfig);
$db = \Config\Database::connect('tenant');
This gives developers the power to switch databases safely and officially.
Backward Compatibility
This method does not change any existing behavior.
It only adds a new optional utility method.
No BC breaks.
Conclusion
This feature will make CodeIgniter 4 significantly more flexible for advanced and enterprise applications that require dynamic DB switching.
Thank you for considering this enhancement! 🙏
Expected Output
I would like to request a new method in Config\Database that allows developers to set or override a database connection instance at runtime without modifying the framework core.
This is especially useful when a project needs dynamic database switching, such as in:
Multi-tenant SaaS applications
ERP systems with client-specific databases
“Switch DB per request” API requirements
Dynamic “connect with custom credentials” scenarios
Currently, CodeIgniter 4 allows Database::connect($group) but there is no official method to replace or inject a database instance for a given group.
Proposed Method
Add this method in system/Database/Config.php:
public static function setInstance(string $group, array $config)
{
static::ensureFactory();
$connection = static::$factory->load($config, $group);
static::$instances[$group] = $connection;
return $connection;
}
Why this is needed
Today, Database::connect() always loads config from app/Config/Database.php.
To use a custom connection (hostname, username, password, database), we must create a fake group name or override config objects — both are hacky.
Dynamic DB switching is common in modern SaaS, ERP, and enterprise applications.
This addition would provide a clean, supported API for runtime DB instance control.
Example Use Case
$dbConfig = [
'DSN' => '',
'hostname' => $tenantHost,
'username' => $tenantUser,
'password' => $tenantPass,
'database' => $tenantDb,
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
];
$connection = \Config\Database::setInstance('tenant', $dbConfig);
$db = \Config\Database::connect('tenant');
This gives developers the power to switch databases safely and officially.
Backward Compatibility
This method does not change any existing behavior.
It only adds a new optional utility method.
No BC breaks.
Conclusion
This feature will make CodeIgniter 4 significantly more flexible for advanced and enterprise applications that require dynamic DB switching.
Thank you for considering this enhancement! 🙏
Anything else?
No response
- 主要語言
- PHP
- 星號
- 6k
- 分支
- 2k
- 平均合併
- 1 天 22 小時
- 30 天內合併 PR
- 59
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
codeigniter4/CodeIgniter4 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 72/100
codeigniter4/CodeIgniter4#10616 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 35/100
codeigniter4/CodeIgniter4#10618 ·
維護者通常 1 天內回覆
-
難度 3/5 1-2 天 新手友好度 62/100
codeigniter4/CodeIgniter4#10617 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 25/100
codeigniter4/CodeIgniter4#10615 · 1 則留言 ·
維護者通常 1 天內回覆
-
bug
難度 4/5 3-5 天 新手友好度 48/100
codeigniter4/CodeIgniter4#10589 · 3 則留言 ·
維護者通常 1 天內回覆
查看 codeigniter4/CodeIgniter4 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 72/100
Automattic/blocks-engine#2803 ·
維護者通常 1 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 72/100
hestiacp/hestiacp#5798 · 2 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 62/100
woocommerce/woocommerce-google-analytics-integration#699 ·
維護者通常 1 天內回覆
-
Sidebar highlights two items when plugin routes share a prefix (/plugin/foo and /plugin/foo-bar)未關閉
難度 2/5 1-3 小時 新手友好度 72/100
getgrav/grav-plugin-admin2#195 ·
-
難度 2/5 1-3 小時 新手友好度 60/100
維護者通常 1 天內回覆