繁体   English   中英

使用控制台命令将数据坚持到数据库中

[英]Doctrine Persist Data into Database with Console Command

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

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中删除了Extended AbstractController。

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

  • 从DatabaseHelper.php中的insertValues函数中删除了以下行: $this->entityManager = $this->getDoctrine()->getManager(); 就像上面一样使用了持久和刷新功能。

当你的命令对依赖DatabaseHelper是对依赖Entity Manager ,创建DatabaseHelper类的服务,与Entity manager通过其构造函数注入(自动装配,或在其服务定义指定)。

然后,按照此处文档,您可以通过构造函数将DatabaseHelper服务注入命令中,因为默认情况下它将自动装配。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM