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

时间:2022-10-16 14:53:57

I have a command which executes some actions that depend on the entity passed in parameter.

我有一个命令执行一些依赖于参数传递的实体的动作。

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.

1 个解决方案

#1


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

我不确定你究竟是什么意思“异步”,但是给定的例子以同步的方式执行命令,这意味着控制器将等到命令完成后才会进入下一个操作。但是如果你需要以异步(后台)方式运行它,你应该使用Process组件http://symfony.com/doc/current/components/process.html

#1


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

我不确定你究竟是什么意思“异步”,但是给定的例子以同步的方式执行命令,这意味着控制器将等到命令完成后才会进入下一个操作。但是如果你需要以异步(后台)方式运行它,你应该使用Process组件http://symfony.com/doc/current/components/process.html