繁体   English   中英

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

[英]Pass parameter to command from controller Symfony2

我有一个命令,该命令执行一些操作,具体取决于传入参数中的实体。

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());
    }

}

?>

所以我的问题是:

  • 如何在checkAlertCommand.php中获取实体作为参数?
  • 如何从控制器调用此命令并将所需的实体作为参数传递?

谢谢。

您不能将实体直接传递到控制台命令。 而不是您应该将实体的“ 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());
    }

}

?>

您应该使用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);
             ...
        }
    }

更新:回答您的问题

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

暂无
暂无

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

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