Alice Fixture:引用已持久化和刷新的实体



我用一个理论Article实体的例子简化了我的问题。我的文章有标题、内容类别和类型

我的灯具写在单独的 YAML 文件中

# category.yml
AppBundleEntityCategory:
    category_1:
        id: 1
        name: foo
    category_2:
        id: 2
        name: bar

# type.yml
AppBundleEntityType:
    type_1:
        id: 1
        name: baz
    type_2:
        id: 2
        name: qux
# article.yml
AppBundleEntityArticle:
    article_1:
        id: 1
        title: "Lorem ipsum dolor sit amet, consectetur"
        content: "Lorem ipsum dolor sit amet, consectetur"
        category: '@category_1'

问题是我有一个教义侦听器Events::preUpdate它更新文章::类型取决于文章::类别

/**
* Doctrine Listener
*/
class ArticleListener
{
   // ...
    private function preUpdate(LifecycleEventArgs $args)
    {
        $article = $args->getEntity();
        // Get type from DB
        $type = $em->getRepository('AppBundle:Type')->find(1)
        $article->setType($type);
    }
}

所以首先加载类别和类型的夹具

class BasicFixtures extends Fixture
{
    public function load(ObjectManager $manager)
    {
        $files = [
            'category' => __DIR__.'/../../../AppBundle/DataFixtures/category.yml',
            'type'     => __DIR__.'/../../../AppBundle/DataFixtures/type.yml',
            //... other yml files
        ];
        $loader    = new NativeLoader();
        $objectSet = $loader->loadFiles($files);
        foreach ($objectSet->getObjects() as $key => $object) {
            $this->addReference($key, $object);
            $manager->persist($object);
        }
        $manager->flush();
    }
}

然后我用DependentFixtureInterface::getDependencies加载我的文章夹具,以确保类别和类型已经加载

class ArticleFixtures extends Fixture implements DependentFixtureInterface
{
    public function load(ObjectManager $manager)
    {
        // This give me my object
        dump($this->getReference('category_1'));

        $files = [
            'article' => __DIR__.'/../../../AppBundle/DataFixtures/article.yml',
            //... other yml files
        ];
        $loader    = new NativeLoader();
        $objectSet = $loader->loadFiles($files);
        foreach ($objectSet->getObjects() as $key => $object) {
            $manager->persist($object);
        }
        $manager->flush();
    }
    public function getDependencies()
    {
        return [
            BasicFixtures::class,
        ];
    }
}

但与此接缝,"@category_1"的引用丢失了

In FixtureNotFoundExceptionFactory.php line 23:
  [NelmioAliceThrowableExceptionGeneratorResolverFixtureNotFoundException]  
  Could not find the fixture "category_1".

我做错了什么?非常感谢您的帮助

你必须在BasicFixture中命名你的引用。

// other fixtures can get this object using the 'admin-user' name
$this->addReference('category_1', $userAdmin);

然后在ArticleFixture中获取该引用,并给出名称。

$object->addCategory($this->getReference('category_1'));
$manager->persist($object);

供参考:

在灯具之间共享对象:

https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html#sharing-objects-between-fixtures

顺序加载夹具文件:

https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html#loading-the-fixture-files-in-order

最新更新