1

Under Symfony4.2, I have a Translate entity (id, gb_name, fr_name) and LocationCountry entity (id, ISO3166-2 name: GB,FR, DE…, translate_id)

I define a CSV file with 255 countries ("GB", "Great Britain", "Angleterre"…) and I want to push it in Translate and LocationCountry entities tables with DataFixture.

I read carefully https://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html#sharing-objects-between-fixtures
and
php create fixtures with automatic relations

src/DataFixtures/TranslateFixtures.php:

if ($csv_handle) {
        while ($item = fgetcsv($csv_handle, $csv_max_line_length, $csv_delimiter, $csv_enclosure)) {
            $obj = new Translate();
            $obj->setGbValue($item[1]);
            $obj->setFrValue($item[2]);
            $this->addReference('country'.$item[0], $obj);
            $manager->persist($obj);
        }
        fclose($csv_handle);
    }

    $manager->flush();

I am not sure addReference should be before flush() ?

src/DataFixtures/LocationCountryFixtures.php:

    if ($csv_handle) {
        while ($item = fgetcsv($csv_handle, $csv_max_line_length, $csv_delimiter, $csv_enclosure)) {
            $translate_country = $this->getReference('country'.$item[0]);
            $obj = new LocationCountry();
            $obj->setIso3166Name($item[0]);
            $obj->setTranslate($translate_country);
            $manager->persist($obj);
        }
        fclose($csv_handle);
    }

    $manager->flush();
}

public function getDependencies() {
    return array(
        Translate::class,
    );
}

If I remove addReference Translate entity is well filled.

But with the code above, it returns error:

In SymfonyFixturesLoader.php line 76:                                                                                                                                             
The "App\Entity\Translate" fixture class is trying to be loaded, but is not available. Make sure this class is defined as a service and tagged with "doctrine.fixture.orm". 

I think to have the right Use:

use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use App\Entity\LocationCountry;
use App\Entity\Translate;

Thank for your help

bcag2
  • 1,988
  • 1
  • 17
  • 31

2 Answers2

3

Doctrine loads the fixture files in alphabetical order; that's why you get an error. You can consider using function getOrder in your fixtures in order to set which one will be load first.

EDIT :

You get an error because you don't provide the right class in your getDependencies method :

public function getDependencies() {
    return array(
        TranslateFixtures::class,
    );
}
titili
  • 207
  • 1
  • 6
  • As I read at https://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html#loading-the-fixture-files-in-order, the `getDependencies` method from `DependentFixtureInterface` should sold that, no? – bcag2 Feb 20 '19 at 08:46
  • @bcag2 Yes, you're right. I didn't know this method. So, I spotted your error. It seems that you didn't provide the right class in your `getDependencies` method. – titili Feb 20 '19 at 09:00
  • it is not `Translate` but `TranslateFixture`, thanks @titili ! – bcag2 Feb 20 '19 at 09:08
0

In this case, I just remove src/DataFixtures/TranslateFixtures.php

and change src/DataFixtures/LocationCountryFixtures.php to do all job:

if ($csv_handle) {
    while ($item = fgetcsv($csv_handle, $csv_max_line_length, $csv_delimiter, $csv_enclosure)) {
            //$translate_country = $this->getReference('country'.$item[0]);
            $country = new LocationCountry();
            $translate_country = new Translate();
            $translate_country->setGbValue($item[1]);
            $translate_country->setFrValue($item[2]);
            $country->setIso3166Name($item[0]);
            $country->setTranslateCountry($translate_country);
            $manager->persist($translate_country);
            $manager->persist($country);
        }
        fclose($csv_handle);
    }
    $manager->flush();
bcag2
  • 1,988
  • 1
  • 17
  • 31