Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load Symfony's config parameters from database (Doctrine)

Tags:

php

symfony

Instead of hard coding the parameters in parameters.yml I am trying to load them from database. Not all parameters in parameters.yml needs to be loaded from database just a few, like api details of paypal

In config.yml I have imported the parameters.php

imports:
    - { resource: parameters.php }

If I add static information in parameters.php like the one below it works fine

$demoName = 'First Last';
$container->setParameter('demoName', $demoName);

However I am not able to fetch information from database table. I thought i should create class and make use of $em = this->getDoctrine()->getManager(); and it should work but it doesn't and i get the error of

Notice: Undefined variable: paypal_data in /opt/lampp/htdocs/services/app/config/parameters.php (which is being imported from "/opt/lampp/htdocs/services/app/config/config.yml").

This is the attempt i made is as following but the code does not seem to go in __construct()

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\ORM\Mapping as ORM;

class parameters extends Controller
{
    public $paypal_data;

    function __construct() {
        $this->indexAction();
    }

    public function indexAction(){

        $em = $this->getDoctrine()->getManager();
        $this->paypal_data = $em->getRepository('featureBundle:paymentGateways')->findAll();

    }

}
$demoName = 'First Last';
$container->setParameter('demoName', $demoName);
$container->setParameter('paypal_data', $this->paypal_data);

Any help will be much appreciated.

like image 342
Shairyar Avatar asked Feb 25 '15 07:02

Shairyar


2 Answers

You are doing wrong things. You need to declare your CompilerPass and add it to the container. After whole container will be loaded... in the compile time you will have access to all services in it.

Just get the entity manager service and query for needed parameters and register them in container.

Step-by-step instruction:

  • Define Compiler pass:

    # src/Acme/YourBundle/DependencyInjection/Compiler/ParametersCompilerPass.php
    class ParametersCompilerPass implements CompilerPassInterface
    {
        public function process(ContainerBuilder $container)
        {
            $em = $container->get('doctrine.orm.default_entity_manager');
            $paypal_params = $em->getRepository('featureBundle:paymentGateways')->findAll();
            $container->setParameter('paypal_data', $paypal_params);
        }
    }
    
  • In the bundle definition class you need to add compiler pass to your container

    # src/Acme/YourBundle/AcmeYourBundle.php
    class AcmeYourBundle extends Bundle
    {
        public function build(ContainerBuilder $container)
        {
            parent::build($container);
    
            $container->addCompilerPass(new ParametersCompilerPass(), PassConfig::TYPE_AFTER_REMOVING);
        }
    }
    
like image 140
Michael Sivolobov Avatar answered Sep 29 '22 22:09

Michael Sivolobov


The solution with CompilerPass didn't work in my case in Symfony 4, it worked only from the controller but not when used in the twig.yaml file:

param_name: '%param_name%'

Since I need to use this parameter in the sidebar, so in all the pages, passing it from the controller is not feasible.

This is what I did, thanks to the fact that you can reference services as global Twig variables.

I have a service, called Settings, that loads the settings directly from the database, simplified:

class Settings
{
    private Connection $db;

    public function __construct(EntityManagerInterface $em)
    {
        $this->db = $this->em->getConnection();
    }

    /**
     * Get a setting value. 
     * If the setting doesn't exist, return the default value specified as the second param
     */
    public function get(string $name, $default=''): string
    {
        $val = $this->db->executeQuery("SELECT `value` FROM `settings` WHERE `key`=?;", [$name])->fetchOne();
        // strict comparison is needed for cases when $val === '0'
        return $val !== false ? $val : $default;
    }

    /**
     * Set a setting value. 
     * If the setting doesn't exist, create it. Otherwise, replace the db value
     */
    public function set(string $name, ?string $value, string $description = '')
    {
        if(is_null($value)) {
            // Remove the key
            $this->db->executeQuery('DELETE FROM settings WHERE `key`=?;', [$name]);
            return;
        }

        $this->db->executeQuery('INSERT INTO settings (`key`, `type`, `value`, `description`) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE `type`=?, `value`=?, `description`=?;',
            [$name, $type, $value, $description, $type, $value, $description]);
    }

Now, I simply added in config/packages/twig.yaml:

twig:
    # ...
    globals:
        # ...
        settings: '@App\Service\Settings'

This is amazing because now I can read any db setting from any template:

{{ dump(settings.get('setting_name')) }}

The Settings class I posted is simplified, it can still be improved for example to avoid making the same query again if you request the same setting more than once, but that's trivial to do (store the fetched keys in a private $fetched array), or a warmup(array $keys) method to fetch many settings with a single query ("SELECT key, value FROM settings WHERE key IN (?);", [$toFetch], [Connection::PARAM_STR_ARRAY])

like image 42
the_nuts Avatar answered Sep 29 '22 21:09

the_nuts