条令通过控制台命令将数据持久化到数据库中



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

In ControllerTrait.php line 338:
Call to a member function has() on null 

DatabaseHelper.php

<?php
namespace AppController;
use AppEntityCurrency;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use AppControllerCurrencyComparison;
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 AppCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
use AppServiceDatabaseHelper;
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 AppService;
use DoctrineORMEntityManager;
use DoctrineORMEntityManagerInterface;
use SymfonyBundleFrameworkBundleControllerAbstractController;

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();函数。你介意提些建议吗?

--更新&解决方案——

  • Removed扩展了DatabaseHelper类中的AbstractController。

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

  • 从DatabaseHelper.php中的insertValues函数中删除了以下行:CCD_ 3,并且刚刚使用了如上所述的持久化和刷新函数。

由于您的命令对DatabaseHelper有依赖关系,而对Entity Manager有依赖关系。因此,将DatabaseHelper类创建为服务,并通过其构造函数注入Entity manager(自动连接,或在其服务定义中指定)。

然后,根据这里的文档,您可以通过构造函数将DatabaseHelper服务注入到命令中,因为它在默认情况下是自动连接的。

我也会考虑而不是扩展AbstractController,因为您的DatabaseHelper范围非常紧凑,不需要整个容器(假设它只是您发布的内容)。只要注射你需要的东西。

相关内容

  • 没有找到相关文章

最新更新