bit3/contao-doctrine-dbal

This package is abandoned and no longer maintained. The author suggests using the ContaoBlackForest/contao-doctrine-dbal package instead.

Doctrine DBAL Bridge for Contao Open Source CMS

Installs: 1 251

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 3

Type:contao-module

1.1.4 2015-06-10 06:53 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:25:16 UTC


README

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'];

		$connection->query('...');
	}
}

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'];
				}
				break;
			default:
				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;.