Doctrinebundle: add support for configuring a result cache for the dbal

Created on 10 Sep 2012  路  6Comments  路  Source: doctrine/DoctrineBundle

i briefly looked into this, but the AbstractDoctrineExtension cache related code is pretty bound to just the ORM. not sure if to just duplicate code .. kinda too late for 2.1 of course ..

Feature Ready to work on

All 6 comments

Is it still possible to address this?

I would be nice to have this functionality

+1

We welcome pull requests

Hi, any news on this??

Thanks.

I hacked around for my project, and this seems to work.
I'm not experienced here, so take it at your own risk:

diff --git a/src/DependencyInjection/DBALResultCachePass.php b/src/DependencyInjection/DBALResultCachePass.php
new file mode 100644
index 000000000..724652385
--- /dev/null
+++ b/src/DependencyInjection/DBALResultCachePass.php
@@ -0,0 +1,20 @@
+<?php declare(strict_types=1);
+
+namespace App\DependencyInjection;
+
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Reference;
+
+final class DBALResultCachePass implements CompilerPassInterface
+{
+    /**
+     * @inheritdoc
+     */
+    public function process(ContainerBuilder $container): void
+    {
+        $connectionDefinition = $container->getDefinition('doctrine.dbal.connection.configuration');
+
+        $connectionDefinition->addMethodCall('setResultCacheImpl', [new Reference('doctrine.orm.default_result_cache')]);
+    }
+}
diff --git a/src/Kernel.php b/src/Kernel.php
index 789685300..8a203bef9 100644
--- a/src/Kernel.php
+++ b/src/Kernel.php
@@ -2,6 +2,7 @@

 namespace App;

+use App\DependencyInjection\DBALResultCachePass;
 use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
 use Symfony\Component\Config\Loader\LoaderInterface;
 use Symfony\Component\Config\Resource\FileResource;
@@ -37,6 +38,8 @@ class Kernel extends BaseKernel

     protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
     {
+        $container->addCompilerPass(new DBALResultCachePass());
+
         $container->addResource(new FileResource($this->getProjectDir() . '/config/bundles.php'));
         $container->setParameter('container.dumper.inline_class_loader', true);
         $confDir = $this->getProjectDir() . '/config';
Was this page helpful?
0 / 5 - 0 ratings