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.
<?php
use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySqlDriver;
return [
'doctrine' => [
'connection' => [
'orm_default' => [
'driverClass' => PDOMySqlDriver::class,
'params' => [
'host' => '127.0.0.1',
'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.
driverClass
key provides the class name to use as a driver to the database. Since we use MySQL
database, we specify the Doctrine\DBAL\Driver\PDOMySql\Driver
class name. 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.
The params
key contains the connection parameters:
host
may be either the domain name or IP address of the database server;user
is the MySQL user name with granted permissions to the database;password
is the secret word for the user name;dbname
is the name of the database.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 theorm_test
subkey below theorm_default
key and fill it with connection parameters.