且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从数据库中加载Symfony的配置参数(Doctrine)

更新时间:2023-11-26 20:34:58

您在做错事.您需要声明您的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);
        }
    }