新保存的项目为什么在id字段中为0?

huangapple go评论78阅读模式
英文:

Why new saved item has 0 in id field?

问题

新创建的项目在尝试将其传递给事件时,新保存的项目在ID字段中具有0。

项目模型是没有自动增量字段的(此ID从其他应用程序传递,并且必须保存):

Schema::create('items', function (Blueprint $table) {
    $table->unsignedSmallInteger('id')->primary();
    $table->boolean('active')->default(false);
});

作业代码:

class RMQItemCreated implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private array $data;
    public function __construct(array $data)
    {
        $this->data = $data;
    }

    public function handle()
    {
        $item = Item::create(['id' => $this->data['id'], 'active' => true]);
        $item->load('itemLikedCounts'); // 加载相关模型

        ItemCreatedEvent::dispatch($item);
    }
}

我在数据库中检查并看到新项目行已添加了有效的ID。但是,在ItemCreatedEvent中检查项目模型的内容时,发现ID等于0。

在模型app/Models/Item.php中:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    protected $table = 'items';
    // protected $primaryKey = 'id'; // 如果取消注释此行 - 结果相同
    public $timestamps = false;

    protected $fillable = ['id', 'active'];
    protected $guarded = [];

    protected static function boot()
    {
        parent::boot();
    }

    public function itemLikedCounts()
    {
        return $this->hasMany(\App\Models\ItemLikedCount::class);
    }
}

我尝试加载关联的itemLikedCounts,但没有帮助...

"laravel/framework": "^9.41",

提前感谢!

英文:

Making job with new item created new saved item has 0 in id field when I try to pass this item in event.

Model item is made without autoincrement field (this id is passed from other app and must be saved):

Schema::create(&#39;items&#39;, function (Blueprint $table) {
    $table-&gt;unsignedSmallInteger(&#39;id&#39;)-&gt;primary();
    $table-&gt;boolean(&#39;active&#39;)-&gt;default(false);
});

Job Code:

class RMQItemCreated implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private array $data ;
    public function __construct(array $data)
    {
        $this-&gt;data = $data;
    }


    public function handle()
    {
        $item = Item::create([&#39;id&#39; =&gt; $this-&gt;data[&#39;id&#39;], &#39;active&#39; =&gt; true]);
        $item-&gt;load(&#39;itemLikedCounts&#39;); // load related models

       ItemCreatedEvent::dispatch($item);
    }

I check in db and see that new item row was added with valid id.
But checking content of a item model in ItemCreatedEvent I see that id === 0

In model app/Models/Item.php :

&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    protected $table = &#39;items&#39;;
//    protected $primaryKey = &#39;id&#39;; // if to uncomment this - the same results
    public $timestamps = false;

    protected $fillable = [&#39;id&#39;, &#39;active&#39;];
    protected $guarded = [];

    protected static function boot()
    {
        parent::boot();
    }


    public function itemLikedCounts()
    {
        return $this-&gt;hasMany(\App\Models\ItemLikedCount::class);
    }

}

I tried to load relation itemLikedCounts, but it did not help...

&quot;laravel/framework&quot;: &quot;^9.41&quot;,

Thanks in advance!

答案1

得分: 2

如果您的键不是自动递增的,您必须在模型中指定如下:

class Item extends Model
{
    protected $table = 'items';
    public $incrementing = false; // 这里

    public $timestamps = false;

    protected $fillable = ['id', 'active'];
    protected $guarded = [];

    protected static function boot()
    {
        parent::boot();
    }

    public function itemLikedCounts()
    {
        return $this->hasMany(\App\Models\ItemLikedCount::class);
    }
}
英文:

If your key is not auto-incrementing you have to specify that in the model:

class Item extends Model
{
    protected $table = &#39;items&#39;;
    public $incrementing = false; // here

    public $timestamps = false;

    protected $fillable = [&#39;id&#39;, &#39;active&#39;];
    protected $guarded = [];

    protected static function boot()
    {
        parent::boot();
    }


    public function itemLikedCounts()
    {
        return $this-&gt;hasMany(\App\Models\ItemLikedCount::class);
    }

}

答案2

得分: 0

声明你的模型中不递增 ID:

public $incrementing = false;

而不是

$item = Item::create(['id' => $this->data['id'], 'active' => true]);

尝试这样做:

$item = new Item();
$item->id = $this->data['id'];
$item->active = true;
$item->save();

这样会传递 ID 给事件!

英文:

Declare that your id is not incrementing in your model:

public $incrementing = false;

Instead of

$item = Item::create([&#39;id&#39; =&gt; $this-&gt;data[&#39;id&#39;], &#39;active&#39; =&gt; true]);

Try this:

$item = new Item();
$item-&gt;id = $this-&gt;data[&#39;id&#39;];
$item-&gt;active = true;
$item-&gt;save();

This should pass the ID to event!

huangapple
  • 本文由 发表于 2023年3月12日 13:52:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定