如何从数据库中加载Symfony的配置参数(Doctrine) [英] How to load Symfony's config parameters from database (Doctrine)

查看:101
本文介绍了如何从数据库中加载Symfony的配置参数(Doctrine)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是在parameters.yml中对参数进行硬编码,而是尝试从数据库中加载它们.并非仅需要从数据库中加载parameter.yml中的所有参数,例如paypal的api详细信息

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

config.yml中,我已导入parameters.php

imports:
    - { resource: parameters.php }

如果我像下面的代码一样在parameters.php中添加静态信息,则效果很好

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

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

但是,我无法从数据库表中获取信息.我以为我应该创建类并利用$em = this->getDoctrine()->getManager();,它应该可以工作,但是不能,并且我得到

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").

这是我所做的尝试,如下所示,但是代码似乎并没有出现在__construct()

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

任何帮助将不胜感激.

推荐答案

您在做错事.您需要声明您的CompilerPass并将其添加到容器中.整个容器加载完毕后,在编译时,您将可以访问其中的所有服务.

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.

分步说明:

  • 定义编译器密码:

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

  • 这篇关于如何从数据库中加载Symfony的配置参数(Doctrine)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆