多对多table与其他一对多table的关系
Relationship between many-to-many table and other one-to-many table
大家早上好,
首先要说的是,我使用Laravel,因此,Eloquent ORM,但我认为它更像是一个纯粹的关系数据库问题(或不是),所以这就是我提到的原因在这里。
有段时间搞了个many-to-many
table和一对多table的关系。
涉及的有这4个table:
- 'machines' (id, name, ...)
- 'products' (id, name, ...)
- 'machine_products' (id, machine_id, product_id, 价格).
- 'restrictions'(id,machine_product_id,天,begin_hour,end_hour)
给定一台机器和一个产品,有 N 个限制。
到目前为止,我认为一切都很好,但是当我想将其转化为 Laravel 模型时,我开始怀疑了。
原则上,我们会有以下三个模型:
- 机器
- 产品
- 限制
- (MachineProduct??? 理论上不需要)
通常情况下,many-to-many
模型不需要制作,因为它们可以通过两个主要模型之一访问。在这种情况下,从 Machine
或 Product
我们可以访问枢轴 table machine_products
.
当我想从 Machine
或 Product
的实例通过 Eloquent 访问 restrictions
时,问题就来了。同样,我不知道如何通过 restrictions
.
的实例访问 Machine
或 Product
一个选项,我现在选择的是这个,虽然只解决了第一个问题:
Restriction::find(Machine::find(1)->products()->first()->pivot->id);
我们可以建议一个更优雅和实用的解决方案,以便从 Product/Machine 和向后获得限制?
谢谢!
编辑
我想要这样的东西:
Machine::find(1)->products()->first()->restrictions
或 Product::find(1)->machines()->first()->[pivot?]->restrictions
.
我也希望能够做到这一点:Restriction::find(1)->[pivot?]->machine
(或产品)
以下是三个模型:
class Machine extends Model
{
public function products()
{
return $this->BelongsToMany('App\Product', 'machine_products')->withPivot('id','price');
}
}
class Product extends Model
{
public function machines()
{
return $this->BelongsToMany('App\Machine', 'machine_products')->withPivot('price');
}
}
class Restriction extends Model
{
// Nothing
}
通常不建议(或不必要)在数据透视 table 中包含 id
列。
我会删除它并调整 restrictions
table:id
、machine_id
、product_id
、...
这就完成了你的第二个要求:Restriction::find(1)->machine
反方向仍然是个问题。我认为对此没有优雅的解决方案:
Restriction::where('machine_id', $machine_id)
->where('product_id', $product_id)
->get();
你可以用一个范围来简化它:
class Restriction extends Model {
public function scopeByIds($query, $machine_id, $product_id) {
$query->where('machine_id', $machine_id)
->where('product_id', $product_id);
}
}
Restriction::byIds($machine_id, $product_id)->get();
大家早上好,
首先要说的是,我使用Laravel,因此,Eloquent ORM,但我认为它更像是一个纯粹的关系数据库问题(或不是),所以这就是我提到的原因在这里。
有段时间搞了个many-to-many
table和一对多table的关系。
涉及的有这4个table:
- 'machines' (id, name, ...)
- 'products' (id, name, ...)
- 'machine_products' (id, machine_id, product_id, 价格).
- 'restrictions'(id,machine_product_id,天,begin_hour,end_hour)
给定一台机器和一个产品,有 N 个限制。
到目前为止,我认为一切都很好,但是当我想将其转化为 Laravel 模型时,我开始怀疑了。
原则上,我们会有以下三个模型:
- 机器
- 产品
- 限制
- (MachineProduct??? 理论上不需要)
通常情况下,many-to-many
模型不需要制作,因为它们可以通过两个主要模型之一访问。在这种情况下,从 Machine
或 Product
我们可以访问枢轴 table machine_products
.
当我想从 Machine
或 Product
的实例通过 Eloquent 访问 restrictions
时,问题就来了。同样,我不知道如何通过 restrictions
.
Machine
或 Product
一个选项,我现在选择的是这个,虽然只解决了第一个问题:
Restriction::find(Machine::find(1)->products()->first()->pivot->id);
我们可以建议一个更优雅和实用的解决方案,以便从 Product/Machine 和向后获得限制?
谢谢!
编辑
我想要这样的东西:
Machine::find(1)->products()->first()->restrictions
或 Product::find(1)->machines()->first()->[pivot?]->restrictions
.
我也希望能够做到这一点:Restriction::find(1)->[pivot?]->machine
(或产品)
以下是三个模型:
class Machine extends Model
{
public function products()
{
return $this->BelongsToMany('App\Product', 'machine_products')->withPivot('id','price');
}
}
class Product extends Model
{
public function machines()
{
return $this->BelongsToMany('App\Machine', 'machine_products')->withPivot('price');
}
}
class Restriction extends Model
{
// Nothing
}
通常不建议(或不必要)在数据透视 table 中包含 id
列。
我会删除它并调整 restrictions
table:id
、machine_id
、product_id
、...
这就完成了你的第二个要求:Restriction::find(1)->machine
反方向仍然是个问题。我认为对此没有优雅的解决方案:
Restriction::where('machine_id', $machine_id)
->where('product_id', $product_id)
->get();
你可以用一个范围来简化它:
class Restriction extends Model {
public function scopeByIds($query, $machine_id, $product_id) {
$query->where('machine_id', $machine_id)
->where('product_id', $product_id);
}
}
Restriction::byIds($machine_id, $product_id)->get();