4

我正在开发的应用程序中有一个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在一个夹具中加载十几个实体。我可以复制和粘贴十几次并更改数据,但这很混乱且难以维护。有没有像这样加载大量相同类型实体的好方法?

4

1 回答 1

3

我意识到教义/数据夹具包已经完全符合我的要求。

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

加载依赖的夹具也很容易:

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'
    );
}
于 2013-03-27T20:16:53.840 回答