脚本宝典收集整理的这篇文章主要介绍了php – 异步子进程中的Symfony2命令,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
class MyCommand extends ContainerAwareCommand{ PRotected function configure() { $this ->setName('my:command') ->setDescription('My command') ->addargument( 'country',Inputargument::required,'which country?' ) ; } protected function execute(InputInterface $input,OutputInterface $output) { $country = $input->getArgument('country'); // OBTain the doctrine manager $dm = $this->getContainer()->get('doctrine_mongodb.odm.document_manager'); $users = $dm->getReposITory('MyBundle:User') ->findBy( array('country'=>$country)); }}
当我从命令行调用它时,它完美地工作:
PHP app/console my:command uk
但是,当我称之为Symfony2进程时,它不起作用:
$process = new Process("PHP ../app/console my:command $country"); $process->start();
我收到一个数据库错误:“[MongoWriteConcernException] 127.0.0.1:27017:not master”
我认为这意味着该过程没有得到我的数据库配置…
也许我需要使用注射?
我使用多种环境:DEV,test和PROD.
我也使用不同的服务器.
因此DEV环境是我自己的机器,具有简单的mongodb配置.
但TEST环境在其他服务器上具有副本集配置…
现在错误得到充分意义:“[MongoWriteConcernException] 127.0.0.1:27017:not master”
为了解决这个问题,我刚刚将环境参数(–env =)添加到流程中,一切都像魅力一样:
$process = new Process("PHP ../app/console my:command $country --env=test");
实际上,为了获得正确的环境,我使用这个:
$this->get('kernel')->getEnvironment();
让我的代码如下:
$process = new Process("PHP ../app/console my:command $country --env=".$this->get('kernel')->getEnvironment());
也许这不是一个美丽的方式,但它适用于我:)
以上是脚本宝典为你收集整理的php – 异步子进程中的Symfony2命令全部内容,希望文章能够帮你解决php – 异步子进程中的Symfony2命令所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。