英文:
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('items', function (Blueprint $table) {
$table->unsignedSmallInteger('id')->primary();
$table->boolean('active')->default(false);
});
Job Code:
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'); // 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 :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
protected $table = 'items';
// protected $primaryKey = 'id'; // if to uncomment this - the same results
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);
}
}
I tried to load relation itemLikedCounts, but it did not help...
"laravel/framework": "^9.41",
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 = 'items';
public $incrementing = false; // here
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);
}
}
答案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(['id' => $this->data['id'], 'active' => true]);
Try this:
$item = new Item();
$item->id = $this->data['id'];
$item->active = true;
$item->save();
This should pass the ID to event!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论