Skip to content

Commit 5f47e92

Browse files
committed
Add new db_connect convenience function.
1 parent 816a911 commit 5f47e92

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

system/Common.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,35 @@ function config(string $name, bool $getShared = true)
102102

103103
//--------------------------------------------------------------------
104104

105+
if (! function_exists('db_connnect'))
106+
{
107+
/**
108+
* Grabs a database connection and returns it to the user.
109+
*
110+
* This is a convenience wrapper for \Config\Database::connect()
111+
* and supports the same parameters. Namely:
112+
*
113+
* When passing in $db, you may pass any of the following to connect:
114+
* - group name
115+
* - existing connection instance
116+
* - array of database configuration values
117+
*
118+
* If $getShared === false then a new connection instance will be provided,
119+
* otherwise it will all calls will return the same instance.
120+
*
121+
* @param \CodeIgniter\Database\ConnectionInterface|array|string $db
122+
* @param bool $getShared
123+
*
124+
* @return \CodeIgniter\Database\BaseConnection
125+
*/
126+
function db_connect($db, bool $getShared = true)
127+
{
128+
return \Config\Database::connect($db, $getShared);
129+
}
130+
}
131+
132+
//--------------------------------------------------------------------
133+
105134
if (! function_exists('view'))
106135
{
107136
/**

user_guide_src/source/database/connecting.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ If the above function does **not** contain any information in the first
1414
parameter it will connect to the default group specified in your database config
1515
file. For most people, this is the preferred method of use.
1616

17+
A convenience method exists that is purely a wrapper around the above line
18+
and is provided for your convenience::
19+
20+
$db = db_connect();
21+
1722
Available Parameters
1823
--------------------
1924

@@ -89,7 +94,7 @@ the same format as the groups are defined in the configuration file::
8994
'failover' => [],
9095
'port' => 3306,
9196
];
92-
$db = \Config\Database::connect($custom);
97+
$db = \Config\Database::connect($custom);
9398

9499

95100
Reconnecting / Keeping the Connection Alive

0 commit comments

Comments
 (0)