1

I have a command connecting to an external database and loading the data into my application's database. The command will run periodically as a cron job. However, I run into the following problem when I run the command in the console:

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    

I followed the tutorial here on symfony's website to the letter.

Here's the service definition:

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

Here's my command code:

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

According to the accepted answer here and many other places online I have seen, extending ContainerAwareCommand should solve this but I still keep getting the error. Please assist in pointing the step I missed, I'll be very grateful

2
  • How have you defined your command? i.e: have you just put it in the Command namespace or have you defined it as a service? are you using the full-stack framework? Commented Sep 4, 2015 at 5:39
  • I am using full-stack framework. I have defined the service. Let me edit question show this Commented Sep 4, 2015 at 6:37

2 Answers 2

3

Remove your service definition, as you put your command inside Command folder and extended ContainerAwareCommand you don't need to use any tags and inject entity manager.

Sign up to request clarification or add additional context in comments.

1 Comment

You're a life saver my friend
0

ContainerAware has been deprecated in 4.2. It's saying now:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.