Laravel: Как я могу использовать фабрики для посева моделей по определенному шаблону? - PullRequest
1 голос
/ 09 июня 2019

У меня есть модель, которая хранит адреса людей. Человек может иметь несколько адресов со следующими условиями:

  • Должен быть один адрес, помеченный как постоянный.
  • Должен быть один адрес, помеченный как текущий.
  • Адрес может быть помечен как постоянный и текущий одновременно.
  • Адрес не может быть ни постоянным, ни текущим.

Я пытаюсь заполнить базу данных, используя фабрики.

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 ]

1 голос
/ 09 июня 2019

Определите фабричные типы отдельно

$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!');
} 
0 голосов
/ 10 июня 2019

Отвечая на мой вопрос:

Принимая подсказку от Erkan Özkök answer Я обновил код, чтобы использовать заводские состояния, это окончательный код.

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

}
0 голосов
/ 09 июня 2019

Я думаю, что самым простым способом было бы сначала вызвать текущий и постоянный адрес, а затем создать случайный.

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
        ]);
    }
});    
...