【问题标题】:Symfony Fixture - 参考不存在
【发布时间】:2022-01-22 02:39:23
【问题描述】:

我尝试在多个文件中编写夹具,我从未通过设置对象创建夹具,所以我尝试关注 SF doc (https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html#loading-the-fixture-files-in-order)

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

LangFixture:

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

【问题讨论】:

    标签: symfony doctrine-orm fixtures


    【解决方案1】:

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

    $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 {
    

    【讨论】:

    • 感谢您的反馈,是的,这是我在测试此语法之前所做的,我忘了改回来。我已经更新了我的代码问题...但我不明白为什么即使使用 self:: 引用也不存在
    • 你需要界面,我已经更新了我的答案:)
    • 我有这个错误:Fixture "App\DataFixtures\LangFixture" was declared as a dependency for fixture "App\DataFixtures\UserFixture", but it was not included in any of the loaded fixture groups. 我记得昨天有同样的错误我试图修复但没有成功,我没有找到任何解决方案,例如我尝试了 getGroups() 等跨度>
    • 基于错误,我怀疑还有其他实现 Doctrine\Bundle\FixturesBundle\FixtureGroupInterface 的装置。您是否尝试过删除该界面?除非您真的想要/需要使用组,否则您应该将接口添加到所有夹具并实现 getGroups() 方法。
    • rigth 现在没有灯具使用组,我刚刚删除了其他灯具的所有依赖项以防万一。但同样的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 2020-03-14
    相关资源
    最近更新 更多