PHP创建具有自动关系的夹具 [英] php create fixtures with automatic relations

查看:63
本文介绍了PHP创建具有自动关系的夹具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在寻找具有大量数据之间关系的网站的固定装置.

I'm currently looking into fixtures for a website with a lot of relations between data.

比方说,有一个与团队表有关的联赛表,与与成员表有关的联赛表.我简要检查了Alice库以处理数据随机化"和其他问题.

Let's say there is a league table which is in relation with teams table which is in relation with members table. I briefly checked Alice library to handle data "randomization" and other stuff.

但是我有一个问题,是否有一个库(或方式),可以让我创建从属"灯具?

But I have a question whether there is a library (or way), that would allow me to create "dependent" fixtures?

例如,我将为联盟设置模板以自动创建20个团队,其中每个团队将基于某个定义的结构自动创建10个成员.

For example I would set template for league to automatically create 20 teams where each team would automatically create 10 members based on some defined structure.

每当我叫创建联赛"夹具时,它都会创建所有依赖项.

Whenever I call create league fixture, it would create all the dependencies.

推荐答案

就像上面所说的Flask,请使用Doctrine DataFixtures.

Like Flask wrote above, use Doctrine DataFixtures.

节省时间,避免使用Alice Bundle/图书馆.一开始我在一个项目上使用了它,但很快就将其删除了.您只是对生成的数据没有足够的控制.

Save yourself time and avoid Alice Bundle / library. I used it on a project in beginning, but had to remove it soon. You simply do not have enough control over generated data.

  • 例如,您希望一个团队中有3个随机球员,一切都还好,但是没有办法说他们必须是唯一的.至少那是我遇到最多问题的方案之一.

但是,是的,使用伪造者库.

But yes, use faker library.

我将给您的另一条建议是,在每个Fixtures类中实现类似-> initSeed()方法的方法,您可以在-> load()方法的开头调用该方法.它会调用这样的内容:

Another advice that I would give you, implement something like a ->initSeed() method in each of your Fixtures class, which you call at the beginning of the ->load() method. It will call something like this:

$this->faker->seed(100);

还要使您的装置实现OrderedFixtureInterface,因此您可以控制首先插入的内容.这就是要抓住的地方……您可以在load()内部而不是在构造函数内部实现initSeed(),因为在所有类的开头都调用了构造函数,因为需要getOrder()方法来找出优先级.这样,如果您在灯具文件的末尾添加另一个随机属性,那么下一个文件仍然会生成相同的数据,因此灯具文件之间将不会相互依赖.

Also make your fixtures implement OrderedFixtureInterface, so you have control what is inserted first. Here's the catch... you implement the initSeed() inside load(), NOT inside constructor, because constructor is called in the beginning on all classes, because getOrder() method is needed to find out priority. This way if you add another randomized property at the end of fixture file, the file which comes next, will still generate the same data, so fixture files will not be dependant between themselves.

如果您需要任何代码示例,请告诉我,我可以提供帮助.

If you need any code example, let me know, I can help.

用于保存参考的代码:

在LoadTeamData夹具类中,具有一个常量:

In the LoadTeamData fixture class, have a constant:

const NUM = 10;

因此,您可以在其他灯具文件中访问它.

So, you can access it in other fixture file.

在for循环中生成团队时,请使用索引$ i保存参考.

When you generate teams in a for loop, use index $i to save the reference.

// Generate teams
for ($i=0; $i<self::NUM; $i++) {
    $team = new Team();
    // ... set things
    $this->setReference('team-'.$i, $team)
    $manager->persist($team);
}

$manager->flush();

然后在LoadPlayerData中添加示例:

Then inside LoadPlayerData for example:

// For each team, generate some players
for ($t=0; $t<LoadTeamData::NUM; $t++) {
    $team = $this->getReference('team-'.$t);
    for ($i=0; $i<self::NUM; $i++) {
        $player = new Player($team);
        // ... set things
        $manager->persist($player);
    }
}

$manager->flush();

这篇关于PHP创建具有自动关系的夹具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆