[Solved] Load Symfony (5.2) config from database


Couple of things going on here. First off, loading config from a database is very unusual in Symfony so it is not surprising that you are having difficulty. Secondly, your process code is never getting called. Part of debugging is making sure that code that you expect to be called is in fact being called. Third, you really got off on a tangent with attempting to add a bundle under config. Way back in Symfony 2 there used to be more bundle related stuff under app/config and it may be that you discovered some old articles and misunderstood them.

But, the big problem here is that Symfony has what is known as a ‘compile’ phase which basically processes all the configuration and caches it. Hence the CompilerPassInterface. Unfortunately, services themselves are not available during the compile phase. They simply don’t exist yet so no entity manager. You need to open your own database connection if you really want to load config from a database. You will want to use just a database connection object and not the entity manager since part of the compile phase is to process the entities themselves.

So get rid of all your code and just adjust your Kernel class:

# src/Kernel.php
class Kernel extends BaseKernel implements CompilerPassInterface
{
    use MicroKernelTrait;

    public function process(ContainerBuilder $container)
    {
        $url = $_ENV['DATABASE_URL'];
        $conn = DriverManager::getConnection(['url' => $url]);

        $settings = $conn->executeQuery('SELECT * FROM settings')->fetchAllAssociative();
        $container->setParameter('test',$settings);
    }

And be aware that even if you get all this working, you will need to manually rebuild the Symfony cache after updating your settings table. It is not going to be automatic. You really might consider taking a completely different approach.

2

solved Load Symfony (5.2) config from database