2016-02-29 46 views
2

我想做一個自定義夾具,它將從csv文件中獲取數據,以特定方式解析它,創建對象,插入數據並將其刷新到數據庫。Symfony3,Doctrine2,定製夾具加載程序

我的問題是,我的文件住在的appbundle /命令,我不知道如何訪問經理堅持和沖洗我的文件。

所以我的問題是:如何訪問主義的經理嗎?

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

這裏是它輸出錯誤

​​

堆棧跟蹤

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

回答

2

你沒有輔助方法getDoctrine()在命令(而不是這個存在於控制器如果從Symfony\Bundle\FrameworkBundle\Controller\Controller inhered)。

嘗試

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

,而不是

$this->getDoctrine() 

希望這有助於

+0

它的工作原理。謝謝 ! – alexpopa