You can get a DBAL Connection through the Doctrine\DBAL\DriverManager
class.
<?php
$config = new \Doctrine\DBAL\Configuration();
//..
$connectionParams = array(
'dbname' => 'mydb',
'user' => 'user',
'password' => 'secret',
'host' => 'localhost',
'driver' => 'pdo_mysql',
);
$conn = DriverManager::getConnection($connectionParams, $config);
The DriverManager
returns an instance of Doctrine\DBAL\Connection
which is a wrapper around the underlying driver connection (which is often a PDO instance).
The following sections describe the available connection parameters in detail.
The driver specifies the actual implementations of the DBAL interfaces to use. It can be configured in one of three ways:
driver
: The built-in driver implementation to use. The following drivers are currently available:pdo_mysql
: A MySQL driver that uses the pdo_mysql PDO extension.pdo_sqlite
: An SQLite driver that uses the pdo_sqlite PDO extension.pdo_pgsql
: A PostgreSQL driver that uses the pdo_pgsql PDO extension.pdo_oci
: An Oracle driver that uses the pdo_oci PDO extension. Note that this driver caused problems in our tests. Prefer the oci8 driver if possible.pdo_sqlsrv
: An MSSQL driver that uses pdo_sqlsrv PDOoci8
: An Oracle driver that uses the oci8 PHP extension.
driverClass
: Specifies a custom driver implementation if no 'driver' is specified. This allows the use of custom drivers that are not part of the Doctrine DBAL itself.pdo
: Specifies an existing PDO instance to use.
By default a Doctrine\DBAL\Connection
is wrapped around a driver Connection
. The wrapperClass
option allows to specify a custom wrapper implementation to use, however, custom wrapper class must be a subclass of Doctrine\DBAL\Connection
.
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.
Note
When using an existing PDO instance through the pdo
option, specifying connection details is obviously not necessary.
user
(string): Username to use when connecting to the database.password
(string): Password to use when connecting to the database.path
(string): The filesystem path to the database file. Mutually exclusive withmemory
.path
takes precedence.memory
(boolean): True if the SQLite database should be in-memory (non-persistent). Mutually exclusive withpath
.path
takes precedence.
user
(string): Username to use when connecting to the database.password
(string): Password to use when connecting to the database.host
(string): Hostname of the database to connect to.port
(integer): Port of the database to connect to.dbname
(string): Name of the database/schema to connect to.unix_socket
(string): Name of the socket used to connect to the database.charset
(string): The charset used when connecting to the database.
user
(string): Username to use when connecting to the database.password
(string): Password to use when connecting to the database.host
(string): Hostname of the database to connect to.port
(integer): Port of the database to connect to.dbname
(string): Name of the database/schema to connect to.
user
(string): Username to use when connecting to the database.password
(string): Password to use when connecting to the database.host
(string): Hostname of the database to connect to.port
(integer): Port of the database to connect to.dbname
(string): Name of the database/schema to connect to.charset
(string): The charset used when connecting to the database.
pdo_sqlsrv ^^^^^^^^^^
user
(string): Username to use when connecting to the database.password
(string): Password to use when connecting to the database.host
(string): Hostname of the database to connect to.port
(integer): Port of the database to connect to.dbname
(string): Name of the database/schema to connect to.
Each built-in driver uses a default implementation of Doctrine\DBAL\Platforms\AbstractPlatform
. 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 4th argument of the PDO constructor.