3. Configuration

3.1. Getting a Connection

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.

3.1.1. Driver

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 PDO
    • oci8:` 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.

3.1.2. Wrapper Class

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.

3.1.3. Connection Details

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.

3.1.3.1. pdo_sqlite

  • 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 with memory. path takes precedence.
  • memory (boolean): True if the SQLite database should be in-memory (non-persistent). Mutually exclusive with path. path takes precedence.

3.1.3.2. pdo_mysql

  • 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.

3.1.3.3. pdo_pgsql

  • 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.

3.1.3.4. pdo_oci / oci8

  • 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.

3.1.3.5. 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.

3.1.4. Custom Platform

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.

3.1.5. Custom Driver Options

The driverOptions option allows to pass arbitrary options through to the driver. This is equivalent to the 4th argument of the PDO constructor.




Fork me on GitHub