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
- Cake\Datasource\ConnectionManager uses Cake\Core\StaticConfigTrait
Properties summary
- A map of connection aliases.
array
- An array mapping url schemes to fully qualified driver class names
array
- The ConnectionRegistry used by the manager.
Inherited Properties
Method Summary
- Set one or more connection aliases.
- Drop an alias.
- Get a connection.
- Parses a DSN into a valid connection configuration
- Configure a new connection object.
Method Detail
alias()source public static
alias( string $alias , string $source )
Set 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.
Throws
Cake\Datasource\Exception\MissingDatasourceConfigException
When aliasing a connection that does not exist.
dropAlias()source public static
dropAlias( string $name )
Drop 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.
get()source public static
get( string $name , boolean $useAliases true )
Get 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.
- boolean
$useAliases
optional true - Set to false to not use aliased connections.
Returns
Cake\Datasource\ConnectionInterface
A connection object.
Throws
Cake\Datasource\Exception\MissingDatasourceConfigException
When config data is missing.
parseDsn()source public static
parseDsn( string|null $config null )
Parses 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|null
$config
optional null - The DSN string to convert to a configuration array
Returns
arrayThe configuration array to be stored after parsing the DSN
setConfig()source public static
setConfig( string|array $key , array|null $config null )
Configure 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|null
$config
optional null - An array of name => config data for adapter.
Throws
Cake\Core\Exception\Exception
When trying to modify an existing config.
See
\Cake\Core\StaticConfigTrait::config()Methods used from Cake\Core\StaticConfigTrait
config()source public static
config( string|array $key , array|null $config null )
This method can be used to define configuration adapters for an application or read existing configuration.
To change an adapter's configuration at runtime, first drop the adapter and then reconfigure it.
Adapters will not be constructed until the first operation is done.
Usage
Assuming that the class' name is Cache
the following scenarios are supported:
Reading config data back:
Cache::config('default');
Setting a cache engine up.
Cache::config('default', $settings);
Injecting a constructed adapter in:
Cache::config('default', $instance);
Configure multiple adapters at once:
Cache::config($arrayOfConfig);
Deprecated
3.4.0 Use setConfig()/getConfig() instead.Parameters
- string|array
$key
- The name of the configuration, or an array of multiple configs.
- array|null
$config
optional null - An array of name => configuration data for adapter.
Returns
array|nullNull when adding configuration or an array of configuration data when reading.
Throws
BadMethodCallExceptionWhen trying to modify an existing config.
configured()source public static
configured( )
Returns an array containing the named configurations
Returns
arrayArray of configurations.
drop()source public static
drop( string $config )
Drops 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
booleanSuccess of the removal, returns false when the config does not exist.
dsnClassMap()source public static
dsnClassMap( array $map null )
Returns or updates the DSN class map for this class.
Deprecated
3.4.0 Use setDsnClassMap()/getDsnClassMap() instead.Parameters
- array
$map
optional null - Additions/edits to the class map to apply.
Returns
arraygetConfig()source public static
getConfig( string $key )
Reads existing configuration.
Parameters
- string
$key
- The name of the configuration.
Returns
array|nullArray of configuration data.
getDsnClassMap()source public static
getDsnClassMap( )
Returns the DSN class map for this class.
Returns
arraysetDsnClassMap()source public static
setDsnClassMap( array $map )
Updates the DSN class map for this class.
Parameters
- array
$map
- Additions/edits to the class map to apply.
Properties detail
$_dsnClassMapsource
protected static array
An array mapping url schemes to fully qualified driver class names
Return
array[ 'mysql' => 'Cake\Database\Driver\Mysql', 'postgres' => 'Cake\Database\Driver\Postgres', 'sqlite' => 'Cake\Database\Driver\Sqlite', 'sqlserver' => 'Cake\Database\Driver\Sqlserver', ]
$_registrysource
protected static Cake\Datasource\ConnectionRegistry
The ConnectionRegistry used by the manager.
© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.4/class-Cake.Datasource.ConnectionManager.html