Symfony Fixture - 参考不存在

Symfony Fixture - Reference does not exist

我尝试在多个文件中编写灯具,我从来没有通过设置对象创建灯具,所以我尝试遵循 SF 文档(https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html#loading-the-fixture-files-in-order)

我有这个错误:Reference to "lang49" does not existlang49 是第一个对象集

语言夹具:

use App\Entity\Lang;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;

class LangFixture extends Fixture
{
    public const ENGLISH_LANG_REFERENCE = 'lang39';
    public const FRENCH_LANG_REFERENCE = 'lang49';
    public const SPANISH_LANG_REFERENCE = 'lang41';
    public const TURKISH_LANG_REFERENCE = 'lang163';

    public function load(ObjectManager $manager): void
    {
...
        $lang39 = new Lang();
        $lang39->setLang("en");
        $lang39->setName("English");
        $lang39->setEnglishName("English");
        $lang39->setEnabled(true);
        $manager->persist($lang39);
        $this->addReference(self::ENGLISH_LANG_REFERENCE, $lang39);
...
        $lang41 = new Lang();
        $lang41->setLang("es");
        $lang41->setName("Español");
        $lang41->setEnglishName("Spanish; Castilian");
        $lang41->setEnabled(true);
        $manager->persist($lang41);
        $this->addReference(self::SPANISH_LANG_REFERENCE, $lang41);
...
        $lang49 = new Lang();
        $lang49->setLang("fr");
        $lang49->setName("Français");
        $lang49->setEnglishName("French");
        $lang49->setEnabled(true);
        $manager->persist($lang49);
        $this->addReference(self::FRENCH_LANG_REFERENCE, $lang49);
...
        $lang163 = new Lang();
        $lang163->setLang("tr");
        $lang163->setName("Türkçe");
        $lang163->setEnglishName("Turkish");
        $lang163->setEnabled(false);
        $manager->persist($lang163);
        $this->addReference(self::TURKISH_LANG_REFERENCE, $lang163);
...
        $manager->flush();
    }
}

用户夹具:

use App\Entity\User;
use App\DataFixtures\LangFixture;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;

class UserFixture extends Fixture
{
    public const ADMIN_USER_REFERENCE = 'admin';
    public const VISITOR_USER_REFERENCE = 'visitor';
    
    public function load(ObjectManager $manager): void
    {
        $admin = new User();
        $admin->setLang($this->getReference(LangFixture::FRENCH_LANG_REFERENCE));
        $admin->addLang($this->getReference(LangFixture::FRENCH_LANG_REFERENCE));
        $admin->addLang($this->getReference(LangFixture::ENGLISH_LANG_REFERENCE));
        $admin->addLang($this->getReference(LangFixture::SPANISH_LANG_REFERENCE));
        $admin->setEmail('example1@mail.com');
        $admin->setRoles(["ROLE_SUPER_ADMIN"]);
        ...
        $admin->setLangContributor(true);
        $admin->addContributorLang($this->getReference(LangFixture::TURKISH_LANG_REFERENCE));
        $admin->setIsVerified(true);
        $manager->persist($admin);
        $this->addReference(self::ADMIN_USER_REFERENCE, $admin);

        $visitor = new User();
        $visitor->setLang($this->getReference(LangFixture::FRENCH_LANG_REFERENCE));
        $visitor->addLang($this->getReference(LangFixture::FRENCH_LANG_REFERENCE));
        $visitor->setEmail('example2@mail.com');
        $visitor->setRoles(["ROLE_SUPER_VISITOR"]);
        ...
        $admin->setLangContributor(false);
        $visitor->setIsVerified(true);
        $manager->persist($visitor);
        $this->addReference(self::VISITOR_USER_REFERENCE, $visitor);
        
        $manager->flush();
    }

    public function getDependencies()
    {
        return [
            LangFixture::class,
        ];
    }
}

然后 UserFixture addReferences 将被用于 MessageFixture...

怎么了?谢谢


已更新,现在可以执行 exepti0n

您正在使用 class 常量作为您的语言参考,但您没有使用它们来设置参考。

$this->addReference('ENGLISH_LANG_REFERENCE', $lang39);
// Results in a reference called "ENGLISH_LANG_REFERENCE"

$this->addReference(self::ENGLISH_LANG_REFERENCE, $lang39);
// Results in a reference called "lang39" since that is the value of your constant

由于您使用的是从属装置,因此您的 UserFixture 应该实现 DependentFixtureInterface

// Old
class UserFixture extends Fixture {

// New
use Doctrine\Common\DataFixtures\DependentFixtureInterface;

class UserFixture extends Fixture implements DependentFixtureInterface {