Symfony3、Doctrine2、自定义夹具加载器

问题描述 投票:0回答:1

我正在尝试制作一个自定义夹具,它将从 csv 文件中获取数据,以特定方式解析它,创建对象,插入数据并将其刷新到数据库。

我的问题是我的文件位于 AppBundle/Command 中,我不知道如何访问 manager 以便 坚持并刷新我的文件。

所以我的问题是:我如何访问原则的经理?

<?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\Output\OutputInterface;
use Doctrine\Common\Persistence\ObjectManager;
use AppBundle\Entity\Country;

class LoadFixturesCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this->setName('app:load-fixtures'));
    }
    protected function execute(InputInterface $input, OutputInterface $output)
    {

        // file path
         $csvfile = '/home/amarui/Documents/studyin/country.csv';
        // if opened
        if (($handle = fopen($csvfile, 'r')) !== false) {

            // read line and breakes at semicolon(;)
            while (($line = fgetcsv($handle, 1000, ';', '"')) !== false) {

                $counter = 0;
                $i = 0; // counter for the array elements
                $city{$counter} = new Country();

                // read elements from breakpoint(;)
                foreach ($line as $value) {
                    // if the element has a comma(,)
                    if (strstr($line[$i], ',')) {
                        // breaks at comma(,)
                        $line[$i] = explode(',', $line[$i]);
                        // reads elements from the breakpoint(,)
                        foreach ($line[$i] as $multiple) {
                        // echo '<br /> count'.$i.' '.$multiple;
                        // TODO: 
                        }
                    } else {
                        // echo '<br /> count'.$i.' '.$value;
                        if ($i = 0) {
                            $city{$counter}->setName($value);
                        }
                    }
                    $i += 1; // next element in the array
                    $counter += 1; // updates the variable name
                    $this->getDoctrine()->getManager()->persist($city{$counter});
                }
            }
        }
        $this->getDoctrine()->getManager()->flush();
    }
}

这是它输出的错误

[Symfony\Component\Debug\Exception\FatalThrowableError]  
  Fatal error: Call to undefined method AppBundle\Comm     
  and\LoadFixturesCommand::getDoctrine()  

堆栈跟踪

[2016-02-29 11:55:44] php.CRITICAL: Fatal error: Call to undefined method AppBundle\Command\LoadFixturesCommand::getDoctrine() {"type":1,"file":"/home/amarui/Dev/student360/src/AppBundle/Command/LoadFixturesCommand.php","line":60,"level":32767,"stack":[{"file":"/home/amarui/Dev/student360/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php","line":256,"function":"execute","class":"AppBundle\\Command\\LoadFixturesCommand","type":"->","args":["[object] (Symfony\\Component\\Console\\Input\\ArgvInput: 'app:load-fixtures')","[object] (Symfony\\Component\\Console\\Output\\ConsoleOutput: {})"]},{"file":"/home/amarui/Dev/student360/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php","line":803,"function":"run","class":"Symfony\\Component\\Console\\Command\\Command","type":"->","args":["[object] (Symfony\\Component\\Console\\Input\\ArgvInput: 'app:load-fixtures')","[object] (Symfony\\Component\\Console\\Output\\ConsoleOutput: {})"]},{"file":"/home/amarui/Dev/student360/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php","line":186,"function":"doRunCommand","class":"Symfony\\Component\\Console\\Application","type":"->","args":["[object] (AppBundle\\Command\\LoadFixturesCommand: {})","[object] (Symfony\\Component\\Console\\Input\\ArgvInput: 'app:load-fixtures')","[object] (Symfony\\Component\\Console\\Output\\ConsoleOutput: {})"]},{"file":"/home/amarui/Dev/student360/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php","line":86,"function":"doRun","class":"Symfony\\Component\\Console\\Application","type":"->","args":["[object] (Symfony\\Component\\Console\\Input\\ArgvInput: 'app:load-fixtures')","[object] (Symfony\\Component\\Console\\Output\\ConsoleOutput: {})"]},{"file":"/home/amarui/Dev/student360/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php","line":117,"function":"doRun","class":"Symfony\\Bundle\\FrameworkBundle\\Console\\Application","type":"->","args":["[object] (Symfony\\Component\\Console\\Input\\ArgvInput: 'app:load-fixtures')","[object] (Symfony\\Component\\Console\\Output\\ConsoleOutput: {})"]},{"file":"/home/amarui/Dev/student360/bin/console","line":29,"function":"run","class":"Symfony\\Component\\Console\\Application","type":"->","args":["[object] (Symfony\\Component\\Console\\Input\\ArgvInput: 'app:load-fixtures')"]}]} 
php symfony doctrine-orm fixtures
1个回答
2
投票

命令中没有辅助方法

getDoctrine()
(相反,如果继承自
Symfony\Bundle\FrameworkBundle\Controller\Controller
,则存在于控制器中)。

尝试使用

$this->getContainer()->get('doctrine')

而不是

$this->getDoctrine()

希望这有帮助

© www.soinside.com 2019 - 2024. All rights reserved.