Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

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

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 SymfonyBundleFrameworkBundleControllerController;
use DoctrineBundleDoctrineBundleRegistry;
use DoctrineORMMapping 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
        }
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...