Master/Slave DBAL connections with Symfony 4From the start Doctrine has been designed to work with multiple connections. Unless separately specified Doctrine always uses the current connection for executing the queries. If you use the: The current connection is the last opened connection. In the next example we dbal close connection show how you vlose get the current connection from the:
Simplifying database interactions with Doctrine DBAL | The Dev Files
The easiest way to specify commonly used connection parameters is using a database URL. The scheme is used to specify a driver, the user and password in the URL encode user and password for the connection, followed by the host and port parts the "authority". The path after the authority part represents the name of the database, sans the leading slash.
Any query parameters are used as additional connection parameters. The scheme names representing the drivers are either the regular driver names see below with any underscores in their name replaced with a hyphen to make them legal in URL scheme names , or one of the following simplified driver names that serve as aliases:.
For connecting to an SQLite database, the authority portion of the URL is obviously irrelevant and thus can be omitted. The path part of the URL is, like for all other drivers, stripped of its leading slash, resulting in a relative file name for the database:. This would access somedb. To specify an absolute file path, e. Any information extracted from the URL overwrites existing values for the parameter in question, but the rest of the information is merged together. You could, for example, have a URL without the charset setting in the query string, and then add a charset connection parameter next to url , to provide a default value in case the URL doesn't contain a charset value.
The driver specifies the actual implementations of the DBAL interfaces to use. It can be configured in one of three ways:. The connection details identify the database to connect to as well as the credentials to use. The connection details can differ depending on the used driver. The following sections describe the options recognized by each built-in driver. When using an existing PDO instance through the pdo option, specifying connection details is obviously not necessary.
To switch from using 'true' and 'false' as strings you can change to integers by using: Depending on the used underlying platform version, you can specify any other connection parameter that is supported by the particular platform version via the driverOptions option. You can find a list of supported connection parameters for each platform version here:.
Doctrine ships with different database platform implementations for some vendors to support version specific features, dialect and behaviour. Otherwise Doctrine always used a default platform implementation. The following drivers support automatic database platform detection out of the box without any extra configuration required:.
Some drivers cannot provide the version of the underlying database server without having to query for it explicitly. For performance reasons to save one extra query on every connect , Doctrine does not enable automatic database platform version detection for the following drivers:. If you still want to tell Doctrine which database server version you are using in order to choose the appropriate platform implementation, you can pass the serverVersion option with a vendor specific version string that matches the database server version you are using.
You can also pass this option if you want to disable automatic database platform detection for a driver that natively supports it and choose the platform version implementation explicitly. If you are running a MariaDB database, you should prefix the serverVersion with mariadb- ex: If you wish to use a customized or custom implementation, you can pass a precreated instance in the platform option. The driverOptions option allows to pass arbitrary options through to the driver.
This is equivalent to the fourth argument of the PDO constructor. Table of Contents master 2. The following sections describe the available connection parameters in detail. The scheme names representing the drivers are either the regular driver names see below with any underscores in their name replaced with a hyphen to make them legal in URL scheme names , or one of the following simplified driver names that serve as aliases: The path part of the URL is, like for all other drivers, stripped of its leading slash, resulting in a relative file name for the database: