Connections
Connections are configured in the core bootstrap file . The Connections::add()
method is used in this file to make different connections available to your models.
Each connection is made up of two parts: the first part is the name that identifies the connection. This must be unique, and can be used to fetch connection details and configure models. If you create a connection named “default”, your models will use it unless configured otherwise.
The second part of a connection is an array of configuration options specific to the database type you’re planning to use. Each connection configuration may differ, but most will include the following:
'type'
string: The type of data source that defines this connection; typically a
class or namespace name. Relational database data sources, use'database'
, while
CouchDB and other HTTP-related data sources use'http'
, etc. For classes which
directly extendlithium\data\Source
, and do not use an adapter, simply use the
name of the class, i.e. .'host'
string: The host name that the database should connect to. Typically
defaults to'localhost'
.'password'
string: If the connection requires authentication, specifies the
password to use.
A quick look at the examples in the default connections.php
file illustrate the possibilities:
For connection-less models you may disable the connection altogether by setting the connection to use to false
.
If you ever need to access the connection for model directly, you may do so by using the method. The following example shows how to make use of this feature to control PDO transactions.
Connections and Environments
Many applications use different databases depending on which environment the application is currently being hosted from. For example, you might want to switch your MongoDB connection from a locally hosted database in development, but use a cloud-based hosting service once you’re in production. Connection configurations were built with this in mind.
Once your environments have been defined, use their names as keys in the configuration array, as shown here: