【问题标题】:Service argument based on console command argument基于控制台命令参数的服务参数
【发布时间】:2015-09-25 18:14:51
【问题描述】:

我有几个像

这样的实体经理
  entity_manager_en
  entity_manager_us
  entity_manager_pl

等等

每个实体管理器引用不同语言内容的数据库。

我有命令,其中需要的参数是 lang(语言)。 我也有作为构造函数参数需要实体管理器的服务。

是否可以根据命令中给出的语言以某种方式动态创建实体管理器,并在创建时将其作为我的服务参数传递?

我尝试使用 ConsoleCommandEvent,但它在输入包含参数之前触发...

【问题讨论】:

  • 向我们展示您已经拥有的代码以及您尝试过的代码怎么样?

标签: php symfony


【解决方案1】:

如果您想在命令中调用execute 方法中的实体管理器,您可以获取参数并将其添加到getManager() 方法中:

protected function execute(InputInterface $input, OutputInterface $output)
{
    $lang = $input->getArgument('lang');

    $em = $this->getContainer()->get('doctrine')->getManager('entity_manager_' . $lang);

    ...
}

【讨论】:

    猜你喜欢
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    相关资源
    最近更新 更多