在 Symfony2 命令中使用 doctrine

Using doctrine in Symfony2 command

我有一个命令连接到外部数据库并将数据加载到我的应用程序的数据库中。该命令将 运行 定期作为 cron 作业。但是,当我在控制台中 运行 命令时,我 运行 遇到了以下问题:

PHP Fatal error:  Call to undefined method Symfony\Component\Console\Application::getKernel() in E:\www\project\vendor\symfony\symfony\src\Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand.php on line 43    

我完全按照 symfony 网站上的教程 here 进行了操作。

这是服务定义:

app.command.get_transactions:
    class: AppBundle\Command\TransactionsCommand
    arguments: [ @doctrine.orm.entity_manager ]
    tags:
        -  { name: console.command }

这是我的命令代码:

<?php

namespace AppBundle\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;
use AppBundle\Entity\Transaction;
use AppBundle\Entity\TransactionSync;
use Doctrine\DBAL\DriverManager;

class TransactionsCommand extends ContainerAwareCommand 
{
    protected function configure()
    {
    $this
        ->setName('transactions:get')
        ->setDescription('Import transactions')
    ;
}


protected function execute(InputInterface $input, OutputInterface $output)
{
    $em = $this->getContainer()->get('doctrine')->getManager();
    $q = $em->createQueryBuilder();
    $q->select('t')->from('AppBundle:TransactionSync', 't')->orderBy('t.id', 'DESC')->setMaxResults(1);
    $sync = $q->getQuery()->getResult();

    $em1 = $this->getContainer()->get('doctrine')->getManager('rnr');
    $conn = $em1->getConnection();
    $query = "SELECT id, merchant, client, phone, traderTransIdent AS member_id, transaction_id, transaction_type_id, value AS amount, points, DATE_FORMAT(STR_TO_DATE( transaction_date, '%d-%m-%Y' ), '%Y-%m-%d') AS transaction_date FROM merchant_transactions WHERE id > ". $sync->getId();
    $stmt = $conn->prepare($query);
    $stmt->execute();
    $results = $stmt->fetchAll();

    if(count($results) > 1)
    {
        $ts = new TransactionSync();
        $ts->setStartTime(new \DateTime());
        $id = 0;
        foreach($results as $result)
        {
            $transaction_type = $em->getRepository('AppBundle:TransactionType')->find($result['transaction_type_id']);
            $member = $em->getRepository('AppBundle:Member')->find($result['member_id']);

            $transaction = new Transaction();
            $transaction->setAmount($result['amount']);
            $transaction->setPoints($result['points']);
            $transaction->setClient($result['client']);
            $transaction->setPhone($result['phone']);
            $transaction->setTransactionId($result['transaction_id']);
            $transaction->setTransactionDate(new \DateTime($result['transaction_date']));
            $transaction->setTransactionType($transaction_type);
            $transaction->setMember($member);
            $em->persist($transaction);
            $id = $result['id'];
        }

        $ts->setLastId($id);
        $ts->setRecords(count($results));
        $ts->setEndTime(new \DateTime());
        $em->persist($ts);
        $em->flush();
    }

    $output->writeln($text);
}
}

根据已接受的答案 here 和我在网上看到的许多其他地方,扩展 ContainerAwareCommand 应该可以解决这个问题,但我仍然不断收到错误消息。请协助指出我遗漏的步骤,将不胜感激

删除您的服务定义,因为您将命令放入 Command 文件夹并扩展 ContainerAwareCommand 您不需要使用任何标签并注入 entity manager.

ContainerAware 已在 4.2 中弃用。现在是说:

The ContainerAwareCommand class has been deprecated. It was used in the past to create commands extending from it so they had direct access to the app service container. The alternative is to extend commands from the Command class and use proper service injection in the command constructor.

https://symfony.com/blog/new-in-symfony-4-2-important-deprecations