我有一个命令连接到外部数据库并将数据加载到我的应用程序的数据库中。该命令将作为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应该解决这个问题,但我仍然会收到错误。请指点我错过的步骤,我将非常感激
答案 0 :(得分:3)
删除服务定义,因为您将命令放在Command
文件夹中并扩展ContainerAwareCommand
,您不需要使用任何标记并注入entity manager
。
答案 1 :(得分:0)
ContainerAware在4.2中已弃用。现在说:
已弃用ContainerAwareCommand类。它用于 过去创建从其扩展的命令,以便它们直接 访问应用程序服务容器。替代方法是扩展 Command类中的命令,并在其中使用适当的服务注入 命令构造器。
https://symfony.com/blog/new-in-symfony-4-2-important-deprecations