bit3 / contao-doctrine-dbal
Requires
- php: >=5.3
- contao-community-alliance/composer-plugin: ~2.0
- contao-community-alliance/dependency-container: ~1
- contao-community-alliance/event-dispatcher: ~1.3
- contao/core: >=2.11.11,<4-dev
- doctrine/dbal: 2.4.*
Suggests
- contaoblackforest/contao-doctrine-dbal-driver: Use doctrine as database driver within Contao.
Replaces
- bit3/contao-doctrine-dbal: 1.1.4
This package is not auto-updated.
Last update: 2022-02-01 12:25:16 UTC
README
此扩展提供了 Doctrine DBAL 到 Contao开源CMS。它只提供了一个服务 $container['doctrine.connection.default']
用于连接默认数据库与Doctrine DBAL。要在Contao数据库框架中使用Doctrine连接,请使用 bit3/contao-doctrine-dbal-driver。
使用Doctrine连接
class MyClass { public function myFunc() { global $container; /** @var \Doctrine\DBAL\Connection $connection */ $connection = $container['doctrine.connection.default']; $connection->query('...'); } }
Contao钩子
$GLOBALS['TL_HOOKS']['prepareDoctrineConnection'] = function(&$connectionParameters, &$config) { ... }
在建立连接之前调用。
$GLOBALS['TL_HOOKS']['doctrineConnect'] = function(&$connection) { ... }
在建立连接之后调用。
定义自定义连接
我们更倾向于使用 依赖注入容器:编写 system/config/services.php
或 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); } );
配置缓存
缓存实现由 $container['doctrine.cache.impl.default']
定义(默认:auto
)。默认情况下,缓存实现会自动检测,按以下顺序尝试:APC、Xcache、memcache、Redis、Array。
可能的设置有
apc | 使用apc缓存 |
---|---|
xcache | 使用xcache缓存 |
memcache://[:] | 在memcache上使用缓存 |
redis://[:] | 在redis上使用缓存 |
redis:// | 在文件上使用redis缓存 |
array | 使用数组缓存 |
缓存生存时间由 $container['doctrine.cache.ttl.default']
定义(默认:0)。
缓存键由 $container['doctrine.cache.key.default']
定义(默认:contao_default_connection
)。
要禁用缓存,设置 $container['doctrine.cache.profile.default'] = null;
。