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
array<string>
A map of connection aliases.
array<string, mixed>
Configuration sets.
array<string, string>
An array mapping url schemes to fully qualified driver class names
Cake\Datasource\ConnectionRegistry
The ConnectionRegistry used by the manager.
Set one or more connection aliases.
Returns an array containing the named configurations
Drops a constructed adapter.
Drop an alias.
Get a connection.
Reads existing configuration.
Reads existing configuration for a specific key.
Returns the DSN class map for this class.
Parses a DSN into a valid connection configuration
Configure a new connection object.
Updates the DSN class map for this class.
alias(string $source, string $alias): void
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().
// Make 'things' resolve to 'test_things' connection ConnectionManager::alias('test_things', 'things');
string
$source The existing connection to alias.
string
$alias The alias name that resolves to $source
.
void
configured(): array<string>
Returns an array containing the named configurations
array<string>
drop(string $config): bool
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.
string
$config An existing configuration you wish to remove.
bool
dropAlias(string $alias): void
Drop an alias.
Removes an alias from ConnectionManager. Fetching the aliased connection may fail if there is no other connection with that name.
string
$alias The connection alias to drop
void
get(string $name, bool $useAliases = true): Cake\Datasource\ConnectionInterface
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.
string
$name The connection name.
bool
$useAliases optional Set to false to not use aliased connections.
Cake\Datasource\ConnectionInterface
Cake\Datasource\Exception\MissingDatasourceConfigException
getConfig(string $key): mixed|null
Reads existing configuration.
string
$key The name of the configuration.
mixed|null
getConfigOrFail(string $key): mixed
Reads existing configuration for a specific key.
The config value for this key must exist, it can never be null.
string
$key The name of the configuration.
mixed
InvalidArgumentException
getDsnClassMap(): array<string, string>
Returns the DSN class map for this class.
array<string, string>
parseDsn(string $config): array<string, mixed>
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.
string
$config The DSN string to convert to a configuration array
array<string, mixed>
setConfig(array<string, mixed>|string $key, object|array<string, mixed>|null $config = null): void
Configure a new connection object.
The connection will not be constructed until it is first used.
array<string, mixed>|string
$key The name of the connection config, or an array of multiple configs.
object|array<string, mixed>|null
$config optional An array of name => config data for adapter.
void
Cake\Core\Exception\CakeException
setDsnClassMap(array<string, string> $map): void
Updates the DSN class map for this class.
array<string, string>
$map Additions/edits to the class map to apply.
void
A map of connection aliases.
array<string>
Configuration sets.
array<string, mixed>
An array mapping url schemes to fully qualified driver class names
array<string, string>
The ConnectionRegistry used by the manager.
Cake\Datasource\ConnectionRegistry
© 2005–present 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/4.4/class-Cake.Datasource.ConnectionManager.html