在Symfony2命令中使用条令

在Symfony2命令中使用条令,symfony,command,Symfony,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\Cont

我有一个连接到外部数据库并将数据加载到应用程序数据库的命令。该命令将作为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网站上的教程

以下是服务定义:

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);
}
}

删除您的服务定义,因为您将命令放在
command
文件夹中,扩展了
ContainerWareCommand
,您不需要使用任何标记并插入
实体管理器

ContainerWare在4.2中已被弃用。现在是这样说的:

ContainerWareCommand类已被弃用。它被用于 过去的做法是创建从它扩展而来的命令,这样它们就可以直接 访问应用程序服务容器。另一种选择是延长 命令类中的命令,并在中使用适当的服务注入 命令构造函数


您是如何定义您的命令的?i、 e:您是将它放在
命令
名称空间中还是将其定义为服务?你使用的是全栈框架吗?我使用的是全栈框架。我已经定义了服务。让我来编辑这个问题,告诉你你是个救生员,我的朋友