我嘗試在多個檔案中撰寫夾具,我從未通過設定物件創建夾具,所以我嘗試遵循 SF 檔案(https://symfony.com/bundles/DoctrineFixturesBundle/current/index.html#loading-the-fixture -按順序排列的檔案)
我有這個錯誤: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('[email protected]');
$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('[email protected]');
$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
uj5u.com熱心網友回復:
您將類常量用于語言參考,但沒有使用它們來設定參考。
$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 {
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/395898.html
