Class ConnectionManager
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
Property Summary
- 
        $_aliasMap protected staticstring[]A map of connection aliases. 
- 
        $_config protected staticarrayConfiguration sets. 
- 
        $_dsnClassMap protected staticstring[]An array mapping url schemes to fully qualified driver class names 
- 
        $_registry protected staticCake\Datasource\ConnectionRegistryThe ConnectionRegistry used by the manager. 
Method Summary
- 
          alias() public staticSet one or more connection aliases. 
- 
          configured() public staticReturns an array containing the named configurations 
- 
          drop() public staticDrops a constructed adapter. 
- 
          dropAlias() public staticDrop an alias. 
- 
          get() public staticGet a connection. 
- 
          getConfig() public staticReads existing configuration. 
- 
          getConfigOrFail() public staticReads existing configuration for a specific key. 
- 
          getDsnClassMap() public staticReturns the DSN class map for this class. 
- 
          parseDsn() public staticParses a DSN into a valid connection configuration 
- 
          setConfig() public staticConfigure a new connection object. 
- 
          setDsnClassMap() public staticUpdates the DSN class map for this class. 
Method Detail
alias() ¶ public static
alias(string $alias, string $source): voidSet one or more connection aliases.
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
- 
                string$alias
- The alias to add. Fetching $source will return $alias when loaded with get. 
- 
                string$source
- The connection to add an alias to. 
Returns
voidThrows
Cake\Datasource\Exception\MissingDatasourceConfigExceptionWhen aliasing a connection that does not exist.
configured() ¶ public static
configured(): string[]Returns an array containing the named configurations
Returns
string[]Array of configurations.
drop() ¶ public static
drop(string $config): boolDrops a constructed adapter.
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
- 
                string$config
- An existing configuration you wish to remove. 
Returns
boolSuccess of the removal, returns false when the config does not exist.
dropAlias() ¶ public static
dropAlias(string $name): voidDrop an alias.
Removes an alias from ConnectionManager. Fetching the aliased connection may fail if there is no other connection with that name.
Parameters
- 
                string$name
- The connection name to remove aliases for. 
Returns
voidget() ¶ public static
get(string $name, bool $useAliases = true): Cake\Datasource\ConnectionInterfaceGet a connection.
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
- 
                string$name
- The connection name. 
- 
                bool$useAliases optional
- Set to false to not use aliased connections. 
Returns
Cake\Datasource\ConnectionInterfaceA connection object.
Throws
Cake\Datasource\Exception\MissingDatasourceConfigExceptionWhen config data is missing.
getConfig() ¶ public static
getConfig(string $key): mixed|nullReads existing configuration.
Parameters
- 
                string$key
- The name of the configuration. 
Returns
mixed|nullConfiguration data at the named key or null if the key does not exist.
getConfigOrFail() ¶ public static
getConfigOrFail(string $key): mixedReads existing configuration for a specific key.
The config value for this key must exist, it can never be null.
Parameters
- 
                string$key
- The name of the configuration. 
Returns
mixedConfiguration data at the named key.
Throws
InvalidArgumentExceptionIf value does not exist.
getDsnClassMap() ¶ public static
getDsnClassMap(): string[]Returns the DSN class map for this class.
Returns
string[]parseDsn() ¶ public static
parseDsn(string $config): arrayParses a DSN into a valid connection configuration
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
- 
                string$config
- The DSN string to convert to a configuration array 
Returns
arrayThe configuration array to be stored after parsing the DSN
setConfig() ¶ public static
setConfig(string|array $key, array|object|null $config = null): voidConfigure a new connection object.
The connection will not be constructed until it is first used.
Parameters
- 
                string|array$key
- The name of the connection config, or an array of multiple configs. 
- 
                array|object|null$config optional
- An array of name => config data for adapter. 
Returns
voidThrows
Cake\Core\Exception\CakeExceptionWhen trying to modify an existing config.
See Also
setDsnClassMap() ¶ public static
setDsnClassMap(string[] $map): voidUpdates the DSN class map for this class.
Parameters
- 
                string[]$map
- Additions/edits to the class map to apply. 
Returns
void