【问题标题】:Symfony - call Doctrine in cron tab [duplicate]Symfony - 在 cron 选项卡中调用 Doctrine [重复]
【发布时间】:2019-04-15 07:23:46
【问题描述】:

嘿,伙计们,我想知道如何做一个 cron 选项卡,但该 cron 选项卡必须更新数据库,所以我需要

$this->getDoctrine()->getManager()

但是为此我需要扩展Controller,但我已经扩展了Command

所以我有办法扩展两者吗?我必须使用 sql 命令吗?或者使用 cron 选项卡更新数据库的其他方式?

在这里你可以找到我的发现,看看我需要什么:

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

class MessageController extends Command
{
    protected function configure () {
        $this->setName('app:notification');
        $this->setDescription("Permet juste d'envoyer les notifications");
        $this->setHelp("Je serai affiche si on lance la commande app/console app:notification -h");
    }

    public function execute (InputInterface $input, OutputInterface $output) {
        //HERE I WANT TO DO : $this->getDoctrine()->getManager();
        $output->writeln('!');
    }
}

【问题讨论】:

    标签: php symfony cron


    【解决方案1】:

    您可以改为扩展 ContainerAwareCommand 并使用

    $this->getContainer()->get('doctrine')->getManager()
    

    【讨论】:

    • 我认为这很有效 :) 我无法接受 9 分钟的响应,所以请稍等一下,我将其设为有效
    • 感谢很多人 :)
    • 避免在 Symfony 4 中直接使用容器:最好通过 DIC 将教义注册表注入到构造函数中。
    猜你喜欢
    • 2014-06-11
    • 2014-02-24
    • 2018-02-03
    • 2017-08-18
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    相关资源
    最近更新 更多