0

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

我的装置是用单独的 YAML 文件编写的

# category.yml
AppBundle\Entity\Category:
    category_1:
        id: 1
        name: foo
    category_2:
        id: 2
        name: bar


# type.yml
AppBundle\Entity\Type:
    type_1:
        id: 1
        name: baz
    type_2:
        id: 2
        name: qux

# article.yml
AppBundle\Entity\Article:
    article_1:
        id: 1
        title: "Lorem ipsum dolor sit amet, consectetur"
        content: "Lorem ipsum dolor sit amet, consectetur"
        category: '@category_1'

问题是我有一个 DoctrineListenerEvents::preUpdate更新 Article::type 取决于 Article::Category

/**
* 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:

  [Nelmio\Alice\Throwable\Exception\Generator\Resolver\FixtureNotFoundException]  
  Could not find the fixture "category_1".

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

4

1 回答 1

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

以供参考:

在 Fixture 之间共享对象:

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

于 2018-01-24T20:29:47.783 回答