【问题标题】:Symfony Cron run ActionSymfony Cron 运行动作
【发布时间】:2015-09-23 20:04:03
【问题描述】:

我有 Symfony 2.6.11 我使用 cron,我需要运行一些脚本

$all_developer = $em->getRepository('ArtelProfileBundle:Developer')->findAll();
foreach ($all_developer as $session_developer) {
    //some logic
}

我知道如何运行 app/console 'somne​​ comand',但如果我需要运行一些代码,我不知道该代码是如何运行的。 帮助如何将此代码添加到应用程序/控制台或任何已解决的问题??

【问题讨论】:

标签: php symfony cron


【解决方案1】:

您可以在此处查看命令示例 (source):

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

【讨论】:

    猜你喜欢
    • 2017-09-20
    • 2019-03-28
    • 2014-12-05
    • 1970-01-01
    • 2013-07-02
    • 2014-11-02
    • 1970-01-01
    相关资源
    最近更新 更多