Symfony3, Doctrine2, custom fixtures loader

alexpopa

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Doctrine2 Symfony2 Custom function parser

From Dev

Symfony 2.8 Doctrine Fixtures

From Dev

Symfony3 Doctrine2 can not access variable in the loop

From Dev

Symfony3 Doctrine2 can not access variable in the loop

From Dev

symfony3 doctrine2 association counter field

From Dev

Symfony3/Doctrine2 : subquery with InnerJoin using QueryBuilder

From Dev

Multiple files for Doctrine Fixtures for Symfony 3

From Dev

Doctrine2 / Symfony 2 @ParamConverter with custom repository method dropping unmapped arguments

From Dev

Doctrine2 / Symfony 2 @ParamConverter with custom repository method dropping unmapped arguments

From Dev

How to getPath of Gedmo Tree element using symfony3, doctrine2 and stofDoctrineExtensionsBundle

From Dev

Symfony2 execute SQL file in Doctrine Fixtures Load

From Dev

Doctrine2 custom records order

From Dev

Doctrine2 custom records order

From Dev

Doctrine2 Entity Custom Joining

From Dev

symfony2 add dynamic parameter for routing, try with custom loader

From Dev

(Symfony 3 / Doctrine 2 ) update of fixtures saves chagens but doesn't get them in WebTestCase

From Dev

CodeIgniter to Symfony2 with Doctrine2

From Dev

Doctrine2 PDO Exception Integrity violation 1048 - Fixtures loading - YML

From Dev

Custom select in a form type in Symfony3

From Dev

symfony 2 Fixtures for many to many relationship

From Dev

Doctrine 2: doctrine:fixtures:load unexpected warning

From Dev

Relation ManyToMany with doctrine/symfony3

From Dev

Right join query in symfony3 with doctrine

From Dev

Right join query in symfony3 with doctrine

From Dev

Symfony3 - Using LEFT or RIGHT with doctrine

From Dev

Add custom SQL to orderBy statment in Doctrine2

From Dev

How to retrieve custom columnDefinition of Doctrine2 Entity?

From Dev

Custom method query findOne($id) for Doctrine2

From Dev

Symfony2 - Doctrine fixtures createdAt showing up as an hour ahead of actual time

Related Related

  1. 1

    Doctrine2 Symfony2 Custom function parser

  2. 2

    Symfony 2.8 Doctrine Fixtures

  3. 3

    Symfony3 Doctrine2 can not access variable in the loop

  4. 4

    Symfony3 Doctrine2 can not access variable in the loop

  5. 5

    symfony3 doctrine2 association counter field

  6. 6

    Symfony3/Doctrine2 : subquery with InnerJoin using QueryBuilder

  7. 7

    Multiple files for Doctrine Fixtures for Symfony 3

  8. 8

    Doctrine2 / Symfony 2 @ParamConverter with custom repository method dropping unmapped arguments

  9. 9

    Doctrine2 / Symfony 2 @ParamConverter with custom repository method dropping unmapped arguments

  10. 10

    How to getPath of Gedmo Tree element using symfony3, doctrine2 and stofDoctrineExtensionsBundle

  11. 11

    Symfony2 execute SQL file in Doctrine Fixtures Load

  12. 12

    Doctrine2 custom records order

  13. 13

    Doctrine2 custom records order

  14. 14

    Doctrine2 Entity Custom Joining

  15. 15

    symfony2 add dynamic parameter for routing, try with custom loader

  16. 16

    (Symfony 3 / Doctrine 2 ) update of fixtures saves chagens but doesn't get them in WebTestCase

  17. 17

    CodeIgniter to Symfony2 with Doctrine2

  18. 18

    Doctrine2 PDO Exception Integrity violation 1048 - Fixtures loading - YML

  19. 19

    Custom select in a form type in Symfony3

  20. 20

    symfony 2 Fixtures for many to many relationship

  21. 21

    Doctrine 2: doctrine:fixtures:load unexpected warning

  22. 22

    Relation ManyToMany with doctrine/symfony3

  23. 23

    Right join query in symfony3 with doctrine

  24. 24

    Right join query in symfony3 with doctrine

  25. 25

    Symfony3 - Using LEFT or RIGHT with doctrine

  26. 26

    Add custom SQL to orderBy statment in Doctrine2

  27. 27

    How to retrieve custom columnDefinition of Doctrine2 Entity?

  28. 28

    Custom method query findOne($id) for Doctrine2

  29. 29

    Symfony2 - Doctrine fixtures createdAt showing up as an hour ahead of actual time

HotTag

Archive