Symfony4如何与实体关系创建数据固定



在symfony4.2下,我有 translate entity(id,gb_name,fr_name)和 locationCountry entity(id,iso3166--2名称:GB,FR,DE…,Translate_id)

我定义了255个国家/地区(" GB","英国"," Angleterre"…)的CSV文件,我想将其推入带有DataFixture的翻译和位置越野实体表中。

我仔细阅读https://symfony.com/doc/current/bundles/doctrinefixturesbundle/index.html#sharing-objects-bets-bet--fixtures

PHP创建具有自动关系的固定装置

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

我不确定 addReference 应该在 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,
    );
}

如果我删除 addReference 翻译实体已充分填充。

但是使用上面的代码,它返回错误:

In SymfonyFixturesLoader.php line 76:                                                                                                                                             
The "AppEntityTranslate" 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". 

我认为有正确的用途:

use DoctrineBundleFixturesBundleFixture;
use DoctrineCommonPersistenceObjectManager;
use DoctrineCommonDataFixturesDependentFixtureInterface;
use AppEntityLocationCountry;
use AppEntityTranslate;

感谢您的帮助

学说按字母顺序加载灯具文件;这就是为什么您会遇到错误的原因。您可以考虑在固定装置中使用函数getOrder以设置首先加载的功能。

编辑:

您会遇到错误,因为您在getDependencies方法中没有提供正确的类:

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

在这种情况下,我只需删除src/datafixtures/translatefixtures.php

和更改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]);
            $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();

相关内容

  • 没有找到相关文章

最新更新