@@ -4,7 +4,7 @@ Database Configuration
44
55.. contents ::
66 :local:
7- :depth: 2
7+ :depth: 3
88
99.. note ::
1010 See :ref: `requirements-supported-databases ` for currently supported database drivers.
@@ -18,6 +18,9 @@ connection values (username, password, database name, etc.). The config
1818file is located at **app/Config/Database.php **. You can also set
1919database connection values in the **.env ** file. See below for more details.
2020
21+ Setting Default Database
22+ ========================
23+
2124The config settings are stored in a class property that is an array with this
2225prototype:
2326
@@ -26,37 +29,45 @@ prototype:
2629The name of the class property is the connection name, and can be used
2730while connecting to specify a group name.
2831
29- .. note :: The default location of the SQLite3 database is in the `` writable`` folder.
32+ .. note :: The default location of the SQLite3 database is in the ** writable** folder.
3033 If you want to change the location, you must set the full path to the new folder.
3134
3235DSN
33- ===
36+ ---
37+
38+ Some database drivers (such as Postgre, OCI8) requires a full DSN string to connect.
39+ But if you do not specify a DSN string for a driver that requires it, CodeIgniter
40+ will try to build it with the rest of the provided settings.
3441
35- Some database drivers (such as PDO, PostgreSQL, Oracle, ODBC) might
36- require a full DSN string to be provided. If that is the case, you
37- should use the 'DSN' configuration setting, as if you're using the
38- driver's underlying native PHP extension, like this:
42+ If you specify a DSN, you should use the ``'DSN' `` configuration setting, as if
43+ you're using the driver's underlying native PHP extension, like this:
3944
4045.. literalinclude :: configuration/002.php
46+ :lines: 11-15
4147
42- .. note :: If you do not specify a DSN string for a driver that requires it, CodeIgniter
43- will try to build it with the rest of the provided settings.
48+ DSN in Universal Manner
49+ ^^^^^^^^^^^^^^^^^^^^^^^
4450
4551You can also set a Data Source Name in universal manner (URL like). In that case DSNs must have this prototype:
4652
4753.. literalinclude :: configuration/003.php
54+ :lines: 11-14
4855
4956To override default config values when connecting with a universal version of the DSN string,
5057add the config variables as a query string:
5158
5259.. literalinclude :: configuration/004.php
60+ :lines: 11-15
61+
62+ .. literalinclude :: configuration/010.php
63+ :lines: 11-15
5364
5465.. note :: If you provide a DSN string and it is missing some valid settings (e.g., the
5566 database character set), which are present in the rest of the configuration
5667 fields, CodeIgniter will append them.
5768
5869Failovers
59- =========
70+ ---------
6071
6172You can also specify failovers for the situation when the main connection cannot connect for some reason.
6273These failovers can be specified by setting the failover for a connection like this:
@@ -65,6 +76,9 @@ These failovers can be specified by setting the failover for a connection like t
6576
6677You can specify as many failovers as you like.
6778
79+ Setting Multiple Databases
80+ ==========================
81+
6882You may optionally store multiple sets of connection
6983values. If, for example, you run multiple environments (development,
7084production, test, etc.) under a single installation, you can set up a
@@ -78,15 +92,15 @@ variable located in the config file:
7892
7993.. literalinclude :: configuration/007.php
8094
81- .. note :: The name ' test' is arbitrary. It can be anything you want. By
82- default we've used the word " default" for the primary connection,
95+ .. note :: The name `` test`` is arbitrary. It can be anything you want. By
96+ default we've used the word `` default `` for the primary connection,
8397 but it too can be renamed to something more relevant to your project.
8498
85- defaultGroup
86- ============
99+ Changing Databases Automatically
100+ ================================
87101
88102You could modify the config file to detect the environment and automatically
89- update the `defaultGroup ` value to the correct one by adding the required logic
103+ update the `` defaultGroup ` ` value to the correct one by adding the required logic
90104within the class' constructor:
91105
92106.. literalinclude :: configuration/008.php
@@ -125,7 +139,7 @@ Explanation of Values:
125139=============== ===========================================================================================================
126140 Name Config Description
127141=============== ===========================================================================================================
128- **dsn ** The DSN connect string (an all-in-one configuration sequence).
142+ **DSN ** The DSN connect string (an all-in-one configuration sequence).
129143**hostname ** The hostname of your database server. Often this is 'localhost'.
130144**username ** The username used to connect to the database. (``SQLite3 `` does not use this.)
131145**password ** The password used to connect to the database. (``SQLite3 `` does not use this.)
0 commit comments