我正在尝试创建一个自定义fixture,它将从csv文件中获取数据,以特定方式解析它,创建对象,插入数据并将其刷新到数据库。
我的问题是我的文件位于 AppBundle / Command ,我不知道如何访问管理器以便保持并刷新我的档案。
所以我的问题是:我如何访问doctrine的经理?
<?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')"]}]}
答案 0 :(得分:2)
命令中没有辅助方法getDoctrine()
(如果从Symfony\Bundle\FrameworkBundle\Controller\Controller
加入,则存在于控制器中。)
尝试
$this->getContainer()->get('doctrine')
而不是
$this->getDoctrine()
希望这个帮助