【问题标题】:Loading dozens of data fixtures in a neat manner以整洁的方式加载数十个数据夹具
【发布时间】:2013-03-23 10:32:36
【问题描述】:

我正在开发的应用中有一个Media 实体,它与其他几个实体有关联:SpeakerTagCategory 等。

在下面的代码中,我展示了我为创建一些测试数据而编写的夹具。设置和分配数据之间的众多关系显然很长。

public function load(ObjectManager $manager)
{
    $videoType = new Mediatype();
    $videoType->setName('video');
    $videoType->setType('video');

    $manager->persist($videoType);

    $speaker1 = new Speaker();
    $speaker1->setName('Joe Bloggs');

    $speaker1->setBiography('Joe Bloggs bio.');

    $manager->persist($speaker1);

    $category1 = new Category();
    $category1->setName('PHP');
    $category1->setSlug('php');

    $manager->persist($category1);

    $tag1 = new Tag();
    $tag1->setName('PHPNW');
    $tag1->setSlug('phpnw');

    $manager->persist($tag1);

    $video1 = new Media();
    $video1->setMediatype($videoType);
    $video1->setSpeakers(
        new ArrayCollection(
            array(
                $speaker1
            )
        )
    );

    $video1->setCategories(
        new ArrayCollection(
            array(
                $category1
            )
        )
    );

    $video1->setTags(
        new ArrayCollection(
            array(
                $tag1
            )
        )
    );

    $video1->setDate(new \Datetime());
    $video1->setCreationDate(new \DateTime());
    $video1->setTitle('My video about PHP');
    $video1->setDescription('A video about PHP!');
    $video1->setContent('http://some.video-url.com');
    $video1->setLength('20:00:00');
    $video1->setRating(2.5);
    $video1->setVisits(100);
    $video1->setLanguage('EN');
    $video1->setHostName('PHP');
    $video1->setHostUrl('php');
    $video1->setStatus('pub');
    $manager->persist($video1);
    $manager->flush();
}

现在我想用真实数据替换这个夹具,并在一个夹具中加载十几个 Media 实体。我可以复制和粘贴十几次并更改数据,但这很混乱且难以维护。有没有像这样加载大量相同类型实体的好方法?

【问题讨论】:

  • 你见过 Symfony 生成器吗?
  • 还有KhepinYAMLFixturesBundle,它允许您在 YAML 文件中定义您的夹具(我相信生成器也会这样做)。
  • 所以也许只是循环这段代码并用github.com/fzaninotto/Faker生成的字符串替换字符串?
  • Faker 看起来不错,但我说的更多是代码布局和可读性,而不是生成夹具的不同方法。

标签: php symfony doctrine fixtures


【解决方案1】:

我意识到doctrine/data-fixtures 捆绑包已经完全符合我的要求。

为此,我将每个实体加载到它们自己的夹具中并执行 $this->addReference('admin-user', $user); 以使用 $this->getReference('admin-user'); 从另一个夹具访问它

加载依赖的fixture也很容易:

public function getDependencies()
{
    // fixture classes that this fixture is dependent on
    return array('MyDataFixtures\MyOtherFixture'); 
}

所以现在我的灯具看起来像这样:

public function load(ObjectManager $manager)
{
    $video1 = new Media();
    $video1->setMediatype($this->getReference('video'));
    $video1->setSpeakers(
        new ArrayCollection(
            array(
                $this->getReference('joe-bloggs')
            )
        )
    );

    $video1->setCategories(
        new ArrayCollection(
            array(
                $this->getReference('php')
            )
        )
    );

    $video1->setTags(
        new ArrayCollection(
            array(
                $this->getReference('phpnw')
            )
        )
    );

    $video1->setDate(new \Datetime());
    $video1->setCreationDate(new \DateTime());
    $video1->setTitle('My video about PHP');
    $video1->setDescription('A video about PHP!');
    $video1->setContent('http://some.video-url.com');
    $video1->setLength('20:00:00');
    $video1->setRating(2.5);
    $video1->setVisits(100);
    $video1->setLanguage('EN');
    $video1->setHostName('PHP');
    $video1->setHostUrl('php');
    $video1->setStatus('pub');

    $manager->persist($video1);
    $manager->flush();
}

/**
 * Load this fixtures dependencies
 * @see https://github.com/doctrine/data-fixtures
 *
 * @return array
 */
public function getDependencies()
{
    return array(
        '...\LoadMediatypeData',
        '...\LoadSpeakerData',
        '...\LoadCategoryData',
        '...\LoadTagData'
    );
}

【讨论】:

  • 请注意,您需要扩展 Doctrine\Common\DataFixtures\AbstractFixture 并实现 Doctrine\Common\DataFixtures\OrderedFixtureInterface 才能使此设置正常工作。
猜你喜欢
  • 2022-10-13
  • 2015-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-20
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多