Laravel 8 -当在工厂中自引用关系时,进程已经用信号“11”发出信号

v1uwarro  于 2023-05-08  发布在  其他
关注(0)|答案(3)|浏览(143)

当在我的CategoryFactory中执行自引用关系时,我得到以下错误'The process has been signaled with signal“11”.'。例如,我有以下代码:
CategoryTest.php

<?php

namespace Tests\Unit\Models\Categories;

use App\Models\Category;
use Tests\TestCase;

class CategoryTest extends TestCase
{
    /**
     * A basic unit test example.
     *
     * @return void
     */
    public function test_it_has_children()
    {
        $category = Category::factory()
            ->has(Category::factory()->count(3), 'children')
            ->create();

        $this->assertInstanceOf(Category::class, $category->children->first());
    }
}

看起来这就是错误的来源。我现在确定这是否是引用hasMany关系的正确方法。
CategoryFactory.php

<?php

namespace Database\Factories;

use App\Models\Category;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class CategoryFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Category::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'name' => $name = $this->faker->unique()->name,
            'slug' => Str::of($name)->slug('-'),
            'parent_id' => Category::factory() // This seems to be causing the error
        ];
    }
}

Category.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'slug',
        'order'
    ];

    public function children()
    {
        return $this->hasMany(Category::class, 'parent_id', 'id');
    }
}

我不知道这个错误可能指向什么

lnxxn5zx

lnxxn5zx1#

关于这条线,你认为这似乎是造成错误。我认为这个错误可能是由递归或类似的东西引起的。尝试将其更改为:

'parent_id' => function () {
   return factory(Category::class)->create()->id;
}
u3r8eeie

u3r8eeie2#

每次你调用Category::factory()->create(),它都会通过调用Category::factory()->create()在内部为它创建一个父级,所以这是一个永恒的循环。
您的子创建逻辑是正确的,并且它们被分配了正确的父。
您可以通过在使用顶级类别时为它分配一个空父级来解决这个问题

$category = Category::factory()
            ->has(Category::factory()->count(3), 'children')
            ->create(['parent_id' => null]);

最好在工厂中更改它,以便自动创建的父类别是顶级类别。

public function definition()
{
    return [
        'name' => $name = $this->faker->unique()->name,
        'slug' => Str::of($name)->slug('-'),
        'parent_id' => Category::factory(['parent_id' => null]) //Here
   ];
}

顺便说一句,如果只提供name参数,工厂的行为会很奇怪。就像子弹代表另一个名字一样

//Currently
Category::factory()->create();
//Creates ['name' => 'Random Name', 'slug' => 'random-name']

Category::factory()->create(['name' => 'Johny Johnson']); 
//Creates ['name' => 'Johny Johnson', 'slug' => 'random-name-not-related-to-name']
//By changing this
public function definition()
{
    return [
            'name' => $this->faker->unique()->name,
            'slug' => fn($data) => Str::of($data['name'])->slug('-'), //Notice the function here
            'parent_id' => Category::factory(['parent_id' => null])
    ];
}

//With the change
Category::factory()->create();
//Creates ['name' => 'Random Name', 'slug' => 'random-name']

Category::factory()->create(['name' => 'Johny Johnson']); 
//Creates ['name' => 'Johny Johnson', 'slug' => 'johny-johnson']
ymdaylpp

ymdaylpp3#

我必须将RefreshDatabase特性添加到我的测试类中

class ExampleTest extends TestCase
{
    use RefreshDatabase;
    

 }

相关问题