phpsymfonydoctrine-ormfixtures

Symfony3, Doctrine2, custom fixtures loader


I am trying to make a custom fixture that will take data from a csv file, parse it in a specific way, create the objects, insert the data and flush it to the database.

My problem is that my file lives in AppBundle/Command and I do not know how to access the manager in order to persist and flush my file.

So my question is: How do i access doctrine's 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();
    }
}

Here is the error that it outputs

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

Stack trace

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

Solution

  • You haven't the helper method getDoctrine() in a command (instead this exists in a controller if inhered from Symfony\Bundle\FrameworkBundle\Controller\Controller).

    try with

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

    instead of

    $this->getDoctrine()
    

    Hope this help