Laravel:如何使用工厂以特定模式播种模型?

时间:2019-06-09 19:09:45

标签: php laravel factory

我有一个存储人员地址的模型。 一个人可以满足以下条件的多个地址:

  • 必须有一个地址标记为永久地址。
  • 必须有一个标记为当前地址的地址。
  • 可以同时将一个地址标记为永久地址和当前地址。
  • 地址不能是永久地址,也不能是当前地址。

我正在尝试使用工厂为数据库添加种子。

PersonAddressFactory:

$factory->define(PersonAddress::class, function (Faker $faker) {
    return [
        'Address' => $faker->buildingNumber . ',' . $faker->streetName . ',' . $faker->streetAddress,
        'Landmark' => $faker->optional()->words(3, true),
        'CityID' => App\Entities\City::inRandomOrder()->first()->CityID,
        'PostalCode' => $faker->postcode,
        'IsPermanent' => ???
        'IsCurrent' => ???
    ];
});

种子:

public function run() {
    $no_of_persons = (int)$this->command->ask('How many persons do you want to create?', 10);
    $this->command->info("Seeding {$no_of_persons} persons");
    $persons = factory(App\Entities\Person::class, $no_of_persons)->create();
    $this->command->info('Persons Created!');

    // Creating addresses for persons
    $this->command->info('Creating Addresses for persons');

    $persons->each(
        function ($person) {
            factory(App\Entities\PersonAddress::class, rand(1, 5))->create(['PersonID' => $person->PersonID]);
        });

    $this->command->info('Addresses Created!');

}

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

分别定义工厂类型

$factory->define(PersonAddress::class, function (Faker $faker) {
    return [
        'Address' => $faker->buildingNumber . ',' . $faker->streetName . ',' . $faker->streetAddress,
        'Landmark' => $faker->optional()->words(3, true),
        'CityID' => App\Entities\City::inRandomOrder()->first()->CityID,
        'PostalCode' => $faker->postcode,
        'IsPermanent' => false,
        'IsCurrent' => false,
    ];
});

$factory->defineAs(PersonAddress::class, 'permanent', function (Faker $faker) {
    $address = $factory->raw(PersonAddress::class);

    return array_merge($address, ['IsPermanent' => true, 'IsCurrent' => false]);
});

$factory->defineAs(PersonAddress::class, 'current', function (Faker $faker) {
    $address = $factory->raw(PersonAddress::class);

    return array_merge($address, ['IsPermanent' => false, 'IsCurrent' => true]);
});

$factory->defineAs(PersonAddress::class, 'permanent_and_current', function (Faker $faker) {
    $address = $factory->raw(PersonAddress::class);

    return array_merge($address, ['IsPermanent' => true, 'IsCurrent' => true]);
});

并使用它们随机生成

public function run() {
    $no_of_persons = (int)$this->command->ask('How many persons do you want to create?', 10);
    $this->command->info("Seeding {$no_of_persons} persons");
    $persons = factory(App\Entities\Person::class, $no_of_persons)->create();
    $this->command->info('Persons Created!');

    // Creating addresses for persons
    $this->command->info('Creating Addresses for persons');

    $persons->each(
        function ($person) {
            if (rand(1, 2) == 1) {
                // make different current and permanent addresses
                factory(App\Entities\PersonAddress::class, 'permanent', rand(1, 5))->create(['PersonID' => $person->PersonID]);
                factory(App\Entities\PersonAddress::class, 'current', rand(1, 5))->create(['PersonID' => $person->PersonID]);
            } else {
                // make same address as permanent and current
                factory(App\Entities\PersonAddress::class, 'permanent_and_current', rand(1, 5))->create(['PersonID' => $person->PersonID]);
            }

            // make some random addresses
            factory(App\Entities\PersonAddress::class, rand(0, 3))->create(['PersonID' => $person->PersonID]);
        });

    $this->command->info('Addresses Created!');
} 

答案 1 :(得分:0)

我认为最简单的方法是先强制使用当前且永久的地址,然后再随机选择地址。

use App\Entities\PersonAddress;

// ...

$persons->each(function ($person) {
    factory(PersonAddress::class)->create(['PersonId' => $person->PersonID, 'permanent' => true, 'current' => true]);
    factory(PersonAddress::class, rand(0, 5))->create(['PersonId' => $person->PersonID]);
});

否则,将像您一样播种种子,然后检查数据库中是否存在永久性电流,如果没有,则创建一个永久性电流。

$persons->each(function ($person) {
    factory(PersonAddress::class, rand(1, 5))->create(['PersonID' => $person->PersonID]);

    $permanentCount = PersonAddress::where('PersonID', $person->PersonId)->where('permanent', true)->count();
    $currentCount = PersonAddress::where('PersonID', $person->PersonId)->where('current', true)->count();

    if (! $permanentCount || ! $currentCount) {
        factory(PersonAddress::class, rand(1, 5))->create([
            'PersonID' => $person->PersonID,
            'permanent' => $permanentCount < 1,
            'current' => $currentCount < 1
        ]);
    }
});    

答案 2 :(得分:0)

回答我自己的问题:

Erkan Özkök答案中获取提示我已经更新了代码以使用工厂状态,这是最终代码。

PersonAddressFactory:

$factory->define(PersonAddress::class, function (Faker $faker) {
    return [
        'Address' => $faker->buildingNumber . ',' . $faker->streetName . ',' . $faker->streetAddress,
        'Landmark' => $faker->optional()->words(3, true),
        'CityID' => App\Entities\City::inRandomOrder()->first()->CityID,
        'PostalCode' => $faker->postcode,
        'IsPermanent' => '0',
        'IsCurrent' => '0',
    ];
});

$factory->state(PersonAddress::class, 'permanent', ['IsPermanent' => '1']);
$factory->state(PersonAddress::class, 'current', ['IsCurrent' => '1']);
$factory->state(PersonAddress::class, 'permanent_and_current', ['IsPermanent' => '1', 'IsCurrent' => '1']);

种子:

public function run() {
    $no_of_persons = (int)$this->command->ask('How many persons do you want to create?', 10);
    $this->command->info("Seeding {$no_of_persons} persons");
    $persons = factory(App\Entities\Person::class, $no_of_persons)->create();
    $this->command->info('Persons Created!');

    // Creating addresses for persons
    $this->command->info('Creating Addresses for persons');

    $persons->each(
        function ($person) {
            if (rand(1, 2) == 1) {
                factory(App\Entities\PersonAddress::class)->state('permanent')->create(['PersonID' => $person->PersonID]);
                factory(App\Entities\PersonAddress::class)->state('current')->create(['PersonID' => $person->PersonID]);
            }
            else {
                factory(App\Entities\PersonAddress::class)->state('permanent_and_current')->create(['PersonID' => $person->PersonID]);
            }
            factory(App\Entities\PersonAddress::class, rand(0, 3))->create(['PersonID' => $person->PersonID]);
        });

    $this->command->info('Addresses Created!');

}