Laravel中的表继承 [英] Table inheritance in Laravel
问题描述
在我的UML图中,我使用
在我的数据库中,我有以下内容。
实体表
--------- ----------
| id |标识符|
-------------------
| 1 | AZX-C56 |
-------------------
| 2 | AZX-C06 |
-------------------
| 3 | MZX-9F1 |
-------------------
| 4 | MZX-X09 |
-------------------
工作表
---------------------- -----------------------
| id | entity_id |名字|姓| .. |
---------------------------------------------
| 1 | 1 |让|米歇尔| .. |
---------------------------------------------
| 2 | 2 |简| Doe | .. |
---------------------------------------------
机床表
----------------------------------------------
| id | entity_id |类型|说明|
----------------------------------------------
| 1 | 3 | XU-09-A | lorem ipsum |
----------------------------------------------
| 2 | 4 | XZ-24-U | lorem ipsum |
----------------------------------------------
我的模型分别是:
class Entity extends Model {
protected $ fillable = ['identifier'];
public function worker(){
return $ this-> hasOne(Worker :: class);
}
public function machine(){
return $ this-> hasOne(Machine :: class);
}
}
class Worker extends Model {
protected $ fillable = ['entity_id','firstname','lastname'];
public function entity(){
return $ this-> belongsTo(Entity :: class);
}
}
class机器扩展模型{
protected $ fillable = ['entity_id','type','description'];
public function entity(){
return $ this-> belongsTo(Entity :: class);
}
}
我的问题是:我在做是的,还是有更好的方法在Laravel中正确地映射我的表?
这不是一个继承。这是一个协会。您正在引用 Entity
表中的ids。
从我看到的id用于 Worker
或 Robot
所以你应该添加约束与 xor
。
As a newbie to laravel, I have a problem with setting up my Laravel models correctly. In my UML Diagram, I'm using Generalization so that I can "inherit" some attributes from the parent table:
In my database, I have the following.
Entity table
-------------------
| id | identifier |
-------------------
| 1 | AZX-C56 |
-------------------
| 2 | AZX-C06 |
-------------------
| 3 | MZX-9F1 |
-------------------
| 4 | MZX-X09 |
-------------------
Worker table
---------------------------------------------
| id | entity_id | firstname | lastname |..|
---------------------------------------------
| 1 | 1 | Jean | Michel |..|
---------------------------------------------
| 2 | 2 | Jane | Doe |..|
---------------------------------------------
Machine table
----------------------------------------------
| id | entity_id | Type | Description |
----------------------------------------------
| 1 | 3 | XU-09-A | lorem ipsum |
----------------------------------------------
| 2 | 4 | XZ-24-U | lorem ipsum |
----------------------------------------------
My models respectively:
class Entity extends Model {
protected $fillable = ['identifier'];
public function worker(){
return $this->hasOne(Worker::class);
}
public function machine(){
return $this->hasOne(Machine::class);
}
}
class Worker extends Model {
protected $fillable = ['entity_id','firstname','lastname'];
public function entity(){
return $this->belongsTo(Entity::class);
}
}
class Machine extends Model {
protected $fillable = ['entity_id','type','description'];
public function entity(){
return $this->belongsTo(Entity::class);
}
}
My question is: Am I doing it right, or is there a better way to map my tables correctly in Laravel ?
This is not an inheritance. It is an association. You are referencing the ids from the Entity
table.
From what I see the id is used for either Worker
or Robot
so you should add the constraint with the xor
.
这篇关于Laravel中的表继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!