【问题标题】:Symfony3, Doctrine2, custom fixtures loaderSymfony3、Doctrine2、自定义夹具加载器
【发布时间】:2016-02-29 11:58:31
【问题描述】:

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

我的问题是我的文件位于 AppBundle/Command 中,我不知道如何访问 ma​​nager持久化和刷新 我的文件。

所以我的问题是:我如何访问学说的经理?

<?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')"]}]} 

【问题讨论】:

    标签: doctrine-orm fixtures symfony


    【解决方案1】:

    您在命令中没有辅助方法 getDoctrine()(如果从 Symfony\Bundle\FrameworkBundle\Controller\Controller 继承,则它存在于控制器中)。

    试试

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

    而不是

    $this->getDoctrine()
    

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多