Doctrine DBAL Bridge for Contao Open Source CMS

1.2.0 2018-09-26 22:28 UTC


This extension provide Doctrine DBAL in the Contao Open Source CMS. It only provide a service $container['doctrine.connection.default'] to connect the default database with Doctrine DBAL. To use the Doctrine Connection within the Contao Database Framework, use bit3/contao-doctrine-dbal-driver.

Use the doctrine connection

class MyClass
	public function myFunc()
		global $container;
		/** @var \Doctrine\DBAL\Connection $connection */
		$connection = $container['doctrine.connection.default'];


Contao hooks

$GLOBALS['TL_HOOKS']['prepareDoctrineConnection'] = function(&$connectionParameters, &$config) { ... } Called before the connection will be established.

$GLOBALS['TL_HOOKS']['doctrineConnect'] = function(&$connection) { ... } Called after the connection is established.

Define a custom connection

We prefer to use the dependency injection container: Write a system/config/services.php or system/modules/.../config/services.php:

$container['doctrine.connection.default'] = $container->share(
	function ($container) {
		$config = new \Doctrine\DBAL\Configuration();

		$connectionParameters = array(
			'dbname'   => $GLOBALS['TL_CONFIG']['dbDatabase'],
			'user'     => $GLOBALS['TL_CONFIG']['dbUser'],
			'password' => $GLOBALS['TL_CONFIG']['dbPass'],
			'host'     => $GLOBALS['TL_CONFIG']['dbHost'],
			'port'     => $GLOBALS['TL_CONFIG']['dbPort'],

		switch (strtolower($GLOBALS['TL_CONFIG']['dbDriver'])) {
			// reuse connection
			case 'doctrinemysql':
				return \Database::getInstance()->getConnection();

			case 'mysql':
			case 'mysqli':
				$connectionParameters['driver']  = 'pdo_mysql';
				$connectionParameters['charset'] = $GLOBALS['TL_CONFIG']['dbCharset'];
				if (!empty($GLOBALS['TL_CONFIG']['dbSocket'])) {
					$connectionParameters['unix_socket'] = $GLOBALS['TL_CONFIG']['dbSocket'];
				throw new RuntimeException('Database driver ' . $GLOBALS['TL_CONFIG']['dbDriver'] . ' not known by doctrine.');

		if (!empty($GLOBALS['TL_CONFIG']['dbPdoDriverOptions'])) {
			$connectionParameters['driverOptions'] = deserialize($GLOBALS['TL_CONFIG']['dbPdoDriverOptions'], true);

		return \Doctrine\DBAL\DriverManager::getConnection($connectionParameters, $config);

Configure caching

The caching implementation is defined in $container['doctrine.cache.impl.default'] (default: auto). By default, the caching implementation is detected by default, try this implementations in order: APC, Xcache, memcache, Redis, Array.

Possible settings are:

apc use apc cache
xcache use xcache cache
memcache://[:] use memcache cache on :
redis://[:] use redis cache on :
redis:// use redis cache on file
array use array cache

The caching time to live is defined in $container['doctrine.cache.ttl.default'] (default: 0).

The caching key is defined in $container['doctrine.cache.key.default'] (default: contao_default_connection).

To disable caching, set $container['doctrine.cache.profile.default'] = null;.