简体   繁体   English

将参数传递给控制器​​Symfony2的命令

[英]Pass parameter to command from controller Symfony2

I have a command which executes some actions that depend on the entity passed in parameter. 我有一个命令,该命令执行一些操作,具体取决于传入参数中的实体。

checkAlertCommand.php: checkAlertCommand.php:

<?php

namespace MDB\PlatformBundle\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class checkAlertCommand extends Command {

    protected function configure() {
        $this
                ->setName('platform:checkAlert')
                ->setDescription('Check the alert in in function of the current advert')
                ->addArgument(
                        'postedAdvert'
        );
    }

    protected function execute(InputInterface $input, OutputInterface $output) {
        $postedAdvert = $input->getArgument('postedAdvert');
        $output->writeln($postedAdvert->getTitre());
    }

}

?>

So my questions are: 所以我的问题是:

  • How to get an entity as argument in the checkAlertCommand.php ? 如何在checkAlertCommand.php中获取实体作为参数?
  • How to call this command from a controller and pass the desired entity as argument? 如何从控制器调用此命令并将所需的实体作为参数传递?

Thanks. 谢谢。

You can't pass an entity directly to a console command. 您不能将实体直接传递到控制台命令。 Instead of you should pass "id" of entity as argument, then use repository and pick up desired entity by its id. 而不是您应该将实体的“ id”作为参数传递,然后使用存储库并通过其id获取所需的实体。

<?php

namespace MDB\PlatformBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class checkAlertCommand extends ContainerAwareCommand {

    protected function configure() {
        $this
                ->setName('platform:checkAlert')
                ->setDescription('Check the alert in in function of the current advert')
                ->addArgument(
                        'postedAdvertId'
        );
    }

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

        $em = $this->getContainer()->get('doctrine')->getManager();
        $repo = $em->getRepository('MDBPlatformBundle:PostedAdvert'); 
        $postedAdvert = $repo->find($postedAdvertId);
        $output->writeln($postedAdvert->getTitre());
    }

}

?>

You should use Process component to run the command inside a controller. 您应该使用Process组件在控制器内运行命令。

use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use MDB\PlatformBundle\Command\checkAlertCommand; 

    class MyController extends Controller 
    {
        public function indexAction()
        {
            // get post $postedAdvertId here
            ....
            $command = new checkAlertCommand();
            $command->setContainer($this->container);
            $input = new ArrayInput(array('postedAdvertId' => $postedAdvertId));
            $output = new NullOutput();
            $result = $command->run($input, $output);
             ...
        }
    }

Update: Answer to your question 更新:回答您的问题

I'm not sure what exactly do you mean "asynchronous", but given example executes the command in synchronous way, so mean the controller will wait till command will be finished and only then will go to next operation. 我不确定“异步”到底是什么意思,但是给定的示例以同步方式执行命令,因此意味着控制器将等到命令完成后才进入下一个操作。 But if you need run it in asynchronous(in background) way,you should use Process component http://symfony.com/doc/current/components/process.html 但是,如果需要以异步方式(在后台)运行它,则应使用流程组件http://symfony.com/doc/current/components/process.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM