A free and open-source book on ZF3 for beginners

12.4. Specifying Database Connection Parameters

Below we provide content of the autoload/local.php file of the Blog web application. This config file contains the application-wide database connection settings for the blog MySQL database that we created earlier in this chapter:

This connection is shared between all modules of the web application. If you want to create module-specific connection, consider adding the key to the module.config.php file instead.

use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySqlDriver;

return [
    'doctrine' => [
        'connection' => [
            'orm_default' => [
                'driverClass' => PDOMySqlDriver::class,
                'params' => [
                    'host'     => '',                    
                    'user'     => 'blog',
                    'password' => '<password>',
                    'dbname'   => 'blog',

Above, we have the doctrine key and connection subkey. The connection subkey contains the orm_default subkey which is the default connection.

For your reference, in table 12.1, you can find several other often used database drivers. Each driver class supports its own set of parameters, so please refer to certain driver's code (and related documentation) for additional information.

Table 12.1. Often Used Database Driver Classes
Method Description
Doctrine\DBAL\Driver\PDOSqlite\Driver SQLite driver using PDO PHP extension.
Doctrine\DBAL\Driver\PDOMySql\Driver MySQL driver using PDO PHP extension.
Doctrine\DBAL\Driver\PDOOracle\Driver Oracle driver using PDO PHP extension.
Doctrine\DBAL\Driver\PDOPgSql\Driver PostgreSQL driver using PDO PHP extension.
Doctrine\DBAL\Driver\PDOSqlsrv\Driver MS SQL Server driver using PDO PHP extension.

Because the autoload/local.php file contains environment-specific parameters, you only store its "distribution template", local.php.dist file, in version control. Each developer in your team then renames the local.php.dist file to local.php and enters his own password instead of the placeholder. The local.php file should not be version controlled, because you usually do not want other people on your team (or other people having access to your code repository) seeing the actual password.

What happens if I need several database connections?

You can easily add more database connections by adding other keys below the orm_default key. For example, let's assume that you have another database for testing purposes. To let Doctrine know about this database, you create the orm_test subkey below the orm_default key and fill it with connection parameters.