[英]Symfony Cron run Action
我有Symfony 2.6.11,我使用cron,需要运行一些脚本
$all_developer = $em->getRepository('ArtelProfileBundle:Developer')->findAll();
foreach ($all_developer as $session_developer) {
//some logic
}
我知道如何运行应用程序/控制台“ somne comand”,但是如果我需要运行一些代码,我不知道该代码如何运行。 帮助如何将此代码添加到应用程序/控制台或任何已解决的??
您可以在此处查看命令示例( 源 ):
<?php
namespace Application\CommandBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManager;
/**
* Class testCommand
* @package Application\CommandBundle\Command
*/
class TestCommand extends ContainerAwareCommand
{
/**
* Configuration of the command
*/
protected function configure()
{
$this
->setName('command:do:something')
->setDescription('This command does something');
}
protected function initialize(InputInterface $input, OutputInterface $output)
{
}
/**
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
*
* @return null|int null or 0 if everything went fine, or an error code
*/
protected function execute(InputInterface $inputInterface, OutputInterface $outputInterface)
{
$outputInterface->writeln(
'This command does something <info>' . $inputInterface->getOption('env') . '</info> environment'
);
$this->getContainer()->get('application_command.test')->doSomething();
$outputInterface->writeln('Done');
}
}
在该示例中,代码获取application_command.test服务,并将运行该服务/类的doSomething()
方法:
$this->getContainer()->get('application_command.test')->doSomething();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.