学说留存数据与控制台命令数据库

时间:2019-02-01 15:51:01

标签: php symfony doctrine-orm

我创建了控制台命令,但是在执行过程中出现以下错误:

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

调试时,注意到在DatabaseHelper.php中的以下行之后出现此错误:

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

我应该更改或寻找什么?

谢谢。

-更新- 我创建了一个服务,并尝试将EntityManager作为构造注入。

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

     }
}

,也更新了Command \ CurrencyComparison.php。但是,当我尝试在CurrencyComparison.php上调用execute函数时,无法到达其$this->databaseHelper->insertValues();函数。您愿意提出任何建议吗?

-更新和解决方案-

  • 从类DatabaseHelper移除了AbstractController的扩展。

  • 更改了DatabaseHelper的__construct并按如下方式注入:(EntityManagerInterface $entityManager)

  • 从DatabaseHelper.php的insertValues函数中删除了以下行: $this->entityManager = $this->getDoctrine()->getManager();,并像上面一样使用了persist和flush函数。

1 个答案:

答案 0 :(得分:2)

由于您的命令依赖于DatabaseHelper,而依赖于Entity Manager,因此请创建DatabaseHelper类作为服务,并通过其插入Entity manager构造函数(自动装配,或在其服务定义中指定)。

然后,根据docs here,您可以通过构造函数将DatabaseHelper服务注入到命令中,因为它将默认为自动接线。

我还考虑扩展AbstractController,因为您的DatabaseHelper的范围非常狭窄,不需要整个容器(假设仅是您发布的内容)。只需注入您所需的内容即可。