ConnectionManager
in package
uses
StaticConfigTrait
Manages and loads instances of Connection
Provides an interface to loading and creating connection objects. Acts as a registry for the connections defined in an application.
Provides an interface for loading and enumerating connections defined in config/app.php
Table of Contents
Properties
- $_aliasMap : array<string|int, string>
- A map of connection aliases.
- $_config : array<string, mixed>
- Configuration sets.
- $_dsnClassMap : array<string, string>
- An array mapping url schemes to fully qualified driver class names
- $_registry : ConnectionRegistry
- The ConnectionRegistry used by the manager.
Methods
- alias() : void
- Set one or more connection aliases.
- configured() : array<string|int, string>
- Returns an array containing the named configurations
- drop() : bool
- Drops a constructed adapter.
- dropAlias() : void
- Drop an alias.
- get() : ConnectionInterface
- Get a connection.
- getConfig() : mixed|null
- Reads existing configuration.
- getConfigOrFail() : mixed
- Reads existing configuration for a specific key.
- getDsnClassMap() : array<string, string>
- Returns the DSN class map for this class.
- parseDsn() : array<string, mixed>
- Parses a DSN into a valid connection configuration
- setConfig() : void
- Configure a new connection object.
- setDsnClassMap() : void
- Updates the DSN class map for this class.
Properties
$_aliasMap
A map of connection aliases.
protected
static array<string|int, string>
$_aliasMap
= []
$_config
Configuration sets.
protected
static array<string, mixed>
$_config
= []
$_dsnClassMap
An array mapping url schemes to fully qualified driver class names
protected
static array<string, string>
$_dsnClassMap
= ['mysql' => \Cake\Database\Driver\Mysql::class, 'postgres' => \Cake\Database\Driver\Postgres::class, 'sqlite' => \Cake\Database\Driver\Sqlite::class, 'sqlserver' => \Cake\Database\Driver\Sqlserver::class]
Tags
$_registry
The ConnectionRegistry used by the manager.
protected
static ConnectionRegistry
$_registry
Methods
alias()
Set one or more connection aliases.
public
static alias(string $source, string $alias) : void
Connection aliases allow you to rename active connections without overwriting the aliased connection. This is most useful in the test-suite for replacing connections with their test variant.
Defined aliases will take precedence over normal connection names. For example, if you alias 'default' to 'test', fetching 'default' will always return the 'test' connection as long as the alias is defined.
You can remove aliases with ConnectionManager::dropAlias().
Usage
// Make 'things' resolve to 'test_things' connection
ConnectionManager::alias('test_things', 'things');
Parameters
- $source : string
-
The existing connection to alias.
- $alias : string
-
The alias name that resolves to
$source
.
configured()
Returns an array containing the named configurations
public
static configured() : array<string|int, string>
Return values
array<string|int, string> —Array of configurations.
drop()
Drops a constructed adapter.
public
static drop(string $config) : bool
If you wish to modify an existing configuration, you should drop it, change configuration and then re-add it.
If the implementing objects supports a $_registry
object the named configuration
will also be unloaded from the registry.
Parameters
- $config : string
-
An existing configuration you wish to remove.
Return values
bool —Success of the removal, returns false when the config does not exist.
dropAlias()
Drop an alias.
public
static dropAlias(string $alias) : void
Removes an alias from ConnectionManager. Fetching the aliased connection may fail if there is no other connection with that name.
Parameters
- $alias : string
-
The connection alias to drop
get()
Get a connection.
public
static get(string $name[, bool $useAliases = true ]) : ConnectionInterface
If the connection has not been constructed an instance will be added
to the registry. This method will use any aliases that have been
defined. If you want the original unaliased connections pass false
as second parameter.
Parameters
- $name : string
-
The connection name.
- $useAliases : bool = true
-
Set to false to not use aliased connections.
Tags
Return values
ConnectionInterface —A connection object.
getConfig()
Reads existing configuration.
public
static getConfig(string $key) : mixed|null
Parameters
- $key : string
-
The name of the configuration.
Return values
mixed|null —Configuration data at the named key or null if the key does not exist.
getConfigOrFail()
Reads existing configuration for a specific key.
public
static getConfigOrFail(string $key) : mixed
The config value for this key must exist, it can never be null.
Parameters
- $key : string
-
The name of the configuration.
Tags
Return values
mixed —Configuration data at the named key.
getDsnClassMap()
Returns the DSN class map for this class.
public
static getDsnClassMap() : array<string, string>
Tags
Return values
array<string, string>parseDsn()
Parses a DSN into a valid connection configuration
public
static parseDsn(string $config) : array<string, mixed>
This method allows setting a DSN using formatting similar to that used by PEAR::DB. The following is an example of its usage:
$dsn = 'mysql://user:pass@localhost/database';
$config = ConnectionManager::parseDsn($dsn);
$dsn = 'Cake\Database\Driver\Mysql://localhost:3306/database?className=Cake\Database\Connection';
$config = ConnectionManager::parseDsn($dsn);
$dsn = 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql';
$config = ConnectionManager::parseDsn($dsn);
For all classes, the value of scheme
is set as the value of both the className
and driver
unless they have been otherwise specified.
Note that query-string arguments are also parsed and set as values in the returned configuration.
Parameters
- $config : string
-
The DSN string to convert to a configuration array
Return values
array<string, mixed> —The configuration array to be stored after parsing the DSN
setConfig()
Configure a new connection object.
public
static setConfig(array<string, mixed>|string $key[, array<string, mixed>|null $config = null ]) : void
The connection will not be constructed until it is first used.
Parameters
- $key : array<string, mixed>|string
-
The name of the connection config, or an array of multiple configs.
- $config : array<string, mixed>|null = null
-
An array of name => config data for adapter.
Tags
setDsnClassMap()
Updates the DSN class map for this class.
public
static setDsnClassMap(array<string, string> $map) : void
Parameters
- $map : array<string, string>
-
Additions/edits to the class map to apply.