0

I created the console command but during the execution I get the following error:

In ControllerTrait.php line 338:

  Call to a member function has() on null 

DatabaseHelper.php

<?php

namespace App\Controller;

use App\Entity\Currency;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use App\Controller\CurrencyComparison;

class DatabaseHelper extends AbstractController
{
    public function insertValues()
    {
        $curFromAPI = new CurrencyComparison();
        $curFromAPI = $curFromAPI->compareCurrencies();

        $date = new \DateTime('@'.strtotime('now'));
        $entityManager = $this->getDoctrine()->getManager();

        $currency = new Currency();
        $currency->setName('USD');
        $currency->setAsk($curFromAPI->getUSD());
        $currency->setLastUpdated($date);
        $entityManager->persist($currency);
        $entityManager->flush();
    }
}

CurrencyComparison.php

<?php

namespace App\Command;

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

use App\Service\DatabaseHelper;

class CurrencyComparison extends Command
{
    private $databaseHelper;

    public function __construct(DatabaseHelper $databaseHelper){
        $this->$databaseHelper = $databaseHelper;

        parent::__construct();
    }

    protected function configure()
    {

        $this
            ->setName('app:currency-comparison')
            ->setDescription('Inserts currencies into the database.')
            ->setHelp('Compares currencies from APIs and inserts the lower rates into the database.')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $this->databaseHelper->insertValues();

        $output->writeln([
            'Currencies are being inserted into the database',
            '============',
            '',
        ]);

        $output->writeln('Done.');
    }
}

When debugging, noticed that I'm getting this error after the following line in DatabaseHelper.php:

$entityManager = $this->getDoctrine()->getManager();

What should I change or should be looking for?

Thanks.

--UPDATE-- I created a Service and tried to inject EntityManager as construct.

App/Service/DatabaseHelper.php

<?php

namespace App\Service;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;


class DatabaseHelper extends AbstractController
{
    private $entityManager;

    public function __construct(EntityManager $entityManager){
        $this->entityManager = $entityManager;
    }

    public function insertValues()
    {

        $curFromAPI = new CurrencyComparison();
        $curFromAPI = $curFromAPI->compareCurrencies();

        $date = new \DateTime('@'.strtotime('now'));
        $this->entityManager = $this->getDoctrine()->getManager();
        return dd($curFromAPI);
        $currency = new Currency();
        $currency->setName('USD');
        $currency->setAsk($curFromAPI->getUSD());
        $currency->setLastUpdated($date);
        $entityManager->persist($currency);

        // actually executes the queries (i.e. the INSERT query)
        $entityManager->flush();

     }
}

and updated Command\CurrencyComparison.php too. But when I'm trying to call execute function on CurrencyComparison.php, I cannot reach its' $this->databaseHelper->insertValues(); function. Would you mind to give any suggestions?

--UPDATE & Solution--

  • Removed extends AbstractController from class DatabaseHelper.

  • Changed __construct of DatabaseHelper and injected as follows: (EntityManagerInterface $entityManager)

  • removed the following line from insertValues function in DatabaseHelper.php: $this->entityManager = $this->getDoctrine()->getManager(); and just used persist and flush functions as like above.

3
  • 1
    You are close but you still don't quite get it. You inject the entity manager which is great but then you are still using $entityManager = $this->getDoctrine()... See the problem? Just use $this->entityManager->persist and flush. And get rid of the AbstractController nonsense completely. Commented Feb 1, 2019 at 22:15
  • Thank you for your effort. I'll check this one now. Commented Feb 1, 2019 at 22:18
  • @Cerad it's fine now. I had to take a break probably. Thanks again. Commented Feb 1, 2019 at 23:37

1 Answer 1

2

As your command has a dependency on DatabaseHelper that has a dependency on the Entity Manager, create the DatabaseHelper class as a service, with the Entity manager injected in via its constructor (autowire it, or specify in its service definition).

then, as per the docs here you can inject your DatabaseHelper service into the command via the constructor as it'll it be autowired by default.

I would also consider not extending the AbstractController as your DatabaseHelper is very tight in scope and wont need the entire container (assuming its only what youve posted). Just inject what you need.

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

2 Comments

Thank you. I'll check this and send feedback marking as answer.
I've updated the post, added DatabaseHelper.php as a service (by creating a new file) and followed up the documentation. Where do I go wrong?

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.