Skip to content

Commit ee8c6c1

Browse files
Improves CommonModel return types and adds DB guide
Updates the `research` and `notWhereInList` functions in `CommonModel` to return `object|null` instead of just `object`, indicating the possibility of no results. Adds a troubleshooting guide to the README for configuring multiple database connection groups in CodeIgniter 4, explaining the root cause of connection errors and providing a solution by defining the database groups in `app/Config/Database.php`.
1 parent 0977e90 commit ee8c6c1

2 files changed

Lines changed: 79 additions & 4 deletions

File tree

README.MD

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
## Features
66

7+
- Multiple Database Connection Groups
78
- **Select records** with flexible conditions (`WHERE`, `OR WHERE`, `LIKE`)
89
- **Insert single or multiple records** into the database
910
- **Update and delete** records based on conditions
@@ -44,6 +45,9 @@
4445
- [Drop Primary Key](#drop-primary-key)
4546
- [Drop Key](#drop-key)
4647
- [Drop Foreign Key](#drop-foreign-key)
48+
- [Troubleshooting: Multiple Database Connection Groups](#troubleshooting-multiple-database-connection-groups)
49+
- [Root Cause](#root-cause)
50+
- [Solution](#solution)
4751
- [License](#license)
4852
- [Author](#author)
4953

@@ -252,6 +256,77 @@ $this->commonModel->drpKey('users', 'my_key_name');
252256
$this->commonModel->drpForeignKey('orders', 'fk_orders_users');
253257
```
254258

259+
## Troubleshooting: Multiple Database Connection Groups
260+
261+
When working with multiple database connections in CodeIgniter 4, defining them in the `.env` file is not sufficient on its own. Here's an example configuration in `.env`:
262+
263+
```ini
264+
database.default.hostname=127.0.0.1
265+
database.default.database=giritliadali
266+
database.default.username=root
267+
database.default.password=root
268+
database.default.DBDriver=MySQLi
269+
database.default.port=3306
270+
271+
database.secondDB.hostname=127.0.0.1
272+
database.secondDB.database=giritliadali_bac
273+
database.secondDB.username=root
274+
database.secondDB.password=root
275+
database.secondDB.DBDriver=MySQLi
276+
database.secondDB.port=3306
277+
```
278+
279+
Attempting to connect using:
280+
281+
```php
282+
$this->db = \Config\Database::connect('secondDB');
283+
```
284+
285+
Will result in the error:
286+
287+
```
288+
"secondDB" is not a valid database connection group.
289+
```
290+
291+
### Root Cause
292+
293+
CodeIgniter 4 does not auto-load database groups other than `default` from the `.env` file. You must explicitly define custom groups like `secondDB` in the `app/Config/Database.php` file.
294+
295+
### Solution
296+
297+
To fix this, add a new public array in your `app/Config/Database.php`:
298+
299+
```php
300+
public array $secondDB = [
301+
'DSN' => '',
302+
'hostname' => 'localhost',
303+
'username' => '',
304+
'password' => '',
305+
'database' => '',
306+
'DBDriver' => 'MySQLi',
307+
'DBPrefix' => '',
308+
'pConnect' => false,
309+
'DBDebug' => true,
310+
'charset' => 'utf8mb4',
311+
'DBCollat' => 'utf8mb4_general_ci',
312+
'swapPre' => '',
313+
'encrypt' => false,
314+
'compress' => false,
315+
'strictOn' => false,
316+
'failover' => [],
317+
'port' => 3306,
318+
'numberNative' => false,
319+
'foundRows' => false,
320+
'dateFormat' => [
321+
'date' => 'Y-m-d',
322+
'datetime' => 'Y-m-d H:i:s',
323+
'time' => 'H:i:s',
324+
],
325+
];
326+
```
327+
328+
Once added, `\Config\Database::connect('secondDB')` will work properly.
329+
255330
## License
256331
257332
This project is licensed under the MIT License. See the [LICENSE](https://opensource.org/license/mit) file for more details.

app/Models/CommonModel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ public function count(string $table, array $where = []): int
372372
* @param string $select Columns to select, separated by commas. Defaults to '*' to select all columns.
373373
* @param array $where Associative array of WHERE conditions.
374374
*
375-
* @return object Returns an object containing the result set.
375+
* @return object|null Returns the row object on success or null if no result is found.
376376
*
377377
* @throws InvalidArgumentException If the parameters are invalid.
378378
* @since 1.0.0
@@ -394,7 +394,7 @@ public function count(string $table, array $where = []): int
394394
*
395395
* // If no records match the criteria, an empty result object is returned.
396396
*/
397-
public function research(string $table, array $like = [], string $select = '*', array $where = []): object
397+
public function research(string $table, array $like = [], string $select = '*', array $where = []): mixed
398398
{
399399
$builder = $this->db->table($table);
400400
return $builder->select($select)->where($where)->like($like)->get()->getResult();
@@ -410,7 +410,7 @@ public function research(string $table, array $like = [], string $select = '*',
410410
* @param array $whereInData An array of values that should be excluded from the results.
411411
* @param string $orderBy Column and direction by which to order the results, defaults to 'queue ASC'.
412412
*
413-
* @return object Returns an object containing the result set.
413+
* @return object|null Returns the row object on success or null if no result is found.
414414
*
415415
* @throws InvalidArgumentException If the parameters are invalid.
416416
* @since 1.1.8
@@ -437,7 +437,7 @@ public function research(string $table, array $like = [], string $select = '*',
437437
* // This will retrieve orders that are not 'canceled' or 'returned', sorted by order date in descending order.
438438
* // If no records match the criteria, an empty result object is returned.
439439
*/
440-
public function notWhereInList(string $table, string $select = '*', array $joins = [], string $whereInKey = '', array $whereInData = [], string $orderBy = 'queue ASC'): object
440+
public function notWhereInList(string $table, string $select = '*', array $joins = [], string $whereInKey = '', array $whereInData = [], string $orderBy = 'queue ASC'): mixed
441441
{
442442
$builder = $this->db->table($table)->select($select);
443443
if (!empty($joins)) {

0 commit comments

Comments
 (0)