Add setInstance() support for runtime database switching
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- mysql, php
- Lĩnh vực
- backend-api-design, databases
Hướng nghiên cứu
Bắt đầu trong system/Database/Config.php và đọc luồng Config::connect() hiện có, bao gồm ensureFactory(), lệnh gọi tải factory và nơi lưu trữ các instance. So sánh hành vi được đề xuất của setInstance() với luồng đó và xác định các trường hợp chấp nhận đối với cấu hình tùy chỉnh và các lệnh gọi connect('tenant') tiếp theo.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- PHP
- Star
- 6k
- Fork
- 2k
- Merge trung bình
- 1 ngày 22 giờ
- Pull request đã merge (30 ngày)
- 59
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của codeigniter4/CodeIgniter4
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
codeigniter4/CodeIgniter4#10616 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
codeigniter4/CodeIgniter4#10618 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 62/100
codeigniter4/CodeIgniter4#10617 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
codeigniter4/CodeIgniter4#10615 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
codeigniter4/CodeIgniter4#10589 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của codeigniter4/CodeIgniter4
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
woocommerce/woocommerce-google-analytics-integration#699 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 60/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
ProfessionalWiki/NeoWiki#1675 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Incorrect documentationĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100