PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, December 28, 2021

[FIXED] Symfony Fixture - Reference does not exist

 December 28, 2021     doctrine-orm, fixtures, symfony     No comments   

Issue

I try to write fixtures in multiple files, I've never created fixtures by setting object, so I try to follow SF doc (https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html#loading-the-fixture-files-in-order)

I have this error: Reference to "lang49" does not exist lang49 being first object set

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

UserFixture:

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,
        ];
    }
}

Then UserFixture addReferences will be use into MessageFixture...

What's wrong? Thanx


UPDATED, now it's ok thanx to exepti0n


Solution

You're using class constants for your languages references, but your are not using them for setting the reference.

$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

Since you are using dependent fixtures your UserFixture should implement DependentFixtureInterface.

// Old
class UserFixture extends Fixture {

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

class UserFixture extends Fixture implements DependentFixtureInterface {


Answered By - exepti0n
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing