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
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.
driverClasskey provides the class name to use as a driver to the database. Since we use MySQL database, we specify the
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.
params key contains the connection parameters:
hostmay be either the domain name or IP address of the database server;
useris the MySQL user name with granted permissions to the database;
passwordis the secret word for the user name;
dbnameis the name of the database.
||SQLite driver using PDO PHP extension.|
||MySQL driver using PDO PHP extension.|
||Oracle driver using PDO PHP extension.|
||PostgreSQL driver using PDO PHP extension.|
||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_defaultkey. For example, let's assume that you have another database for testing purposes. To let Doctrine know about this database, you create the
orm_testsubkey below the
orm_defaultkey and fill it with connection parameters.