Laravel Virgin: 设置工厂序列以确保已创建指定数据集

Laravel Virgin: Set factory sequence in order to ensure that a specified dataset has been created

在我的应用程序中,我有以下迁移:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateGridTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('grid', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('width');
            $table->unsignedInteger('height');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('grid');
    }
}
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRoverTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('rover', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('grid_id')->unsigned();
            $table->string('command');
            $table->foreign('grid_id')->references('id')->on('grid');
            $table->smallInteger('last_commandPos')->unsigned()->default(0);
            $table->smallInteger('grid_pos_x')->unsigned();
            $table->smallInteger('grid_pos_y')->unsigned();
            $table->enum('rotation', App\Constants\RoverConstants::ORIENTATIONS);
            $table->string('last_command');

            Schema::enableForeignKeyConstraints();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('rover');
    }
}

正在显式创建表 gridrover。我想通过工厂填充数据:

/** @var \Illuminate\Database\Eloquent\Factory $factory */
use App\Model\Grid;
use App\Model\Rover;
use App\Constants\RoverConstants;
use Faker\Generator as Faker;

/**
 * Random Command Generator based upon:
 * 
 * @param integer $length How many characters the wommand will contain.
 * @return string
 */
function generateRandomCommand($length = 10): string {
    return substr(str_shuffle(str_repeat($x=implode('',RoverConstants::AVAILABLE_COMMANDS), ceil($length/strlen($x)) )),1,$length);
}

$factory->define(Grid::class,function(Faker $faker){
    return [
        'width'=>rand(1,10),
        'height'=>rand(1,10)
    ];
});

$factory->define(Rover::class, function(Faker $faker) {
    $command = generateRandomCommand(rand(0));
    $commandLength = strlen($command);
    $commandPos = rand(0,$commandLength);
    $lastExecutedCommand = substr($command,$commandPos,$commandPos);

    $randomGrid=Grid::inRandomOrder();

    return [
        'grid_id' => $randomGrid->value('id'),
        'grid_pos_x' => rand(0,$randomGrid->value('width')),
        'grid_pos_y' => rand(0,$randomGrid->value('height')),
        'rotation' => RoverConstants::ORIENTATION_EAST,
        'command' => $command,
        'last_commandPos' => $commandPos,
        'last_command' => $lastExecutedCommand,
    ];
});

但是我如何确保 $randomGrid=Grid::inRandomOrder(); 总是 return 一个网格?换句话说,我想检查是否没有网格,然后致电 Grid 工厂从 Rover 工厂制造一个。

你知道我该怎么做吗?

回答不够,仅供参考!

如果你想确保那里总是有一个网格,你可以在那个地方创建一个(调用 GridFactory)。如果你想使用现有的网格,你可以覆盖这个属性。像这样:

$factory->define(Rover::class, function(Faker $faker) {
    $command = generateRandomCommand(rand(0));
    $commandLength = strlen($command);
    $commandPos = rand(0,$commandLength);
    $lastExecutedCommand = substr($command,$commandPos,$commandPos);

    //This will create a second entry in the database.
    $randomGrid = factory(Grid::class)->create();

    return [
        'grid_id' => $randomGrid->id, //no need for the value() method, they are all attributes
        'grid_pos_x' => rand(0,$randomGrid->width), 
        'grid_pos_y' => rand(0,$randomGrid->height),
        'rotation' => RoverConstants::ORIENTATION_EAST,
        'command' => $command,
        'last_commandPos' => $commandPos,
        'last_command' => $lastExecutedCommand,
    ];
});

上面的工厂显示了每当调用 RoverFactory 时动态创建的网格。使用 factory(Rover::class)->create() 创建一个新的流动站也将创建一个新的网格(并保存它)。现在如果你想使用现有的网格,你可以执行以下操作:

factory(Rover::class)->create([
    'grid_id' => $existingGrid->id,
    'grid_pos_x' => rand(0, $existingGrid->width),
    'grid_pos_y' => rand(0, $existingGrid->height),
]);

这将创建一个带有现有网格的漫游者(您之前可能已经使用 GridFactory 或其他东西创建了它。希望这对您有用。享受 Laravel!

编辑:

通常你会这样做:

$factory->define(Rover::class, function(Faker $faker) {
    $command = generateRandomCommand(rand(0));
    $commandLength = strlen($command);
    $commandPos = rand(0,$commandLength);
    $lastExecutedCommand = substr($command,$commandPos,$commandPos);

    return [
        'grid_id' => function() {
            return factory(Grid::class)->create()->id;
        }
        'grid_pos_x' => '', //But then i got nothing for this.
        'grid_pos_y' => '', //But then i also got nothing for this.
        'rotation' => RoverConstants::ORIENTATION_EAST,
        'command' => $command,
        'last_commandPos' => $commandPos,
        'last_command' => $lastExecutedCommand,
    ];
});