lithium\data\Connections::get()
Read the configuration or access the connections you have set up.
Usage:
// Gets the names of all available configurations
$configurations = Connections::get();
// Gets the configuration array for the connection named 'db'
$config = Connections::get('db', ['config' => true]);
// Gets the instance of the connection object, configured with the settings defined for
// this object in Connections::add()
$dbConnection = Connections::get('db');
// Gets the connection object, but only if it has already been built.
// Otherwise returns null.
$dbConnection = Connections::get('db', ['autoCreate' => false]);
Parameters
-
string
$name
The name of the connection to get, as defined in the first parameter of
add()
, when the connection was initially created. -
array
$options
Options to use when returning the connection:
'autoCreate'
: Iffalse
, the connection object is only returned if it has already been instantiated by a previous call.'config'
: Iftrue
, returns an array representing the connection's internal configuration, instead of the connection itself.
Returns
mixedA configured instance of the connection, or an array of the configuration used.
Source
public static function get($name = null, array $options = []) {
static $mockAdapter;
$defaults = ['config' => false, 'autoCreate' => true];
$options += $defaults;
if ($name === false) {
if (!$mockAdapter) {
$class = Libraries::locate('data.source', 'Mock');
$mockAdapter = new $class();
}
return $mockAdapter;
}
if (!$name) {
return array_keys(static::$_configurations);
}
if (!isset(static::$_configurations[$name])) {
return null;
}
if ($options['config']) {
return static::_config($name);
}
$settings = static::$_configurations[$name];
if (!isset($settings[0]['object']) && !$options['autoCreate']) {
return null;
}
return static::adapter($name);
}