gpt4 book ai didi

php - Laravel Eloquent 多对多模型

转载 作者:行者123 更新时间:2023-11-29 13:07:57 26 4
gpt4 key购买 nike

我在 laravel 4 中做一个项目,我已经停下来了。

任务表:

task_id (PK)
task_title

测量表:
measure_id (PK)
measure_title

例程表:
routine_id (PK)
date
time
value
emp_id (FK)

emp表:
emp_id (PK)
first_name
last_name
user_name
email
user_type

现在我很困惑的是我将如何以 Eloquent 方式模拟这些关系,因为我似乎无法弄清楚。在 phpmyadmin DB,我目前有两个表将任务和测量值连接到具有 task_routine 的例程: task_id (PK) , routine_id (PK)measure_routine : measure_id (PK) , routine_id(PK) .

希望当我写这篇文章时,它就像我脑海中的声音一样清晰,感谢您的帮助!

最佳答案

首先在您的所有表格中更改 PK来自 *_id 的字段只需 id .如果您想为一个表与另一个表建立关系,那么约定是,您应该使用父表的 name_id 在子表中保留一个外键。 (名称应为单数形式),例如,建立 emps 之间的关系和 routines您应该使用的表emp_id routines 中的外键表和您的emps表应包含 id PK .所以,你的 emps表是父表,routinesemps 的 child 并且在两个表中的记录应该是这样的(可以使用自定义约定):

emps ( parent ):

id (pk) | first_name | more fields ...
1 | Mr | ...
2 | Miss | ...
3 | Mrs | ...

routines ( child ):
id (pk) | emp_id (FK) | more fields ...
1 | 1 | ... - Child of emps table's first record
2 | 3 | ... - Child of emps table's third record
3 | 3 | ... - Child of emps table's third record

在这里,每个 idemps routines 中用作外键的表 emp_id 中的表 field 。所以, emps表在 routines 中有三个相关记录表和 emps 中的第一条记录表与 routines 中的第一条记录相关表和 emps 中的第三条记录表(id 为 3 )在 routines 中有两条相关记录表(第二个和第三个),因为两条记录都包含 id 3emps emp_id 中表的第三条记录 field 。

现在使用 Laravel 建立关系:

如果您的父表在子表中只有一个相关表,则使用 one-to-one ,例如,如果 ID 为 1 的记录(第一条记录)在 emps表在 routines 中只有一条相关记录使用 emp_id 的表值为 1 的字段如果它不能出现在 routines 中的多个记录中表然后是 one-to-one关系,并且在上面给出的示例记录中不是 one-to-one关系,因为在 routines表中有两条相同的记录 id/pkemps表可用,所以它转到 one-to-many关系,所以可以读作 emps记录有很多 routines .建立 one-to-many我们可以使用的关系:
class Emp extends Eloquent {
public function routines()
{
return $this->hasMany('Routine');
}
}
Routine模型:
class Routine extends Eloquent {
public function emp()
{
return $this->belongsTo('Emp');
}
}

one-to-many父表可能包含多个具有相同 id 的子记录的关系但是如果子表中的记录也有很多 parent ,那么它应该是 many-to-many关系。例如,如果我们想在例程表中建立一个类似这样的关系(不可能两次使用相同的主键):
id (pk) | emp_id (FK) | more fields ...
1 | 1 | ...
2 | 2 | ... - Record 2(id) has parent 2(emp_id)
2 | 3 | ... - Record 2(id) has also parent 3(emp_id)

在这种情况下,这是一个 many-to-many关系,不能使用相同的 id/PK在一张表中两次,所以我们需要构建 many-to-many emps 之间的关系表和 routines表使用第三个表(称为 pivot 表)来维护 many-to-many关系。所以,它看起来像下表:

表名应为 emp_routine但还有其他方法可以使用
不同的名称,但遵循此约定。
emp_routine数据透视表:
id (pk) | emp_id (id/PK of emps) | routine_id (id/PK of routines) | more fields ...
1 | 1 | 1 | ...
2 | 2 | 1 | ...
3 | 1 | 2 | ...

在这里, parent 和 child 在两个表中都有多个相关记录,并且这个 pivot表保持关系。

使用 Laravel 建立关系我们可以使用:
class Emp extends Eloquent {
public function routines()
{
return $this->belongsToMany('Routine');
}
}
Routine模型:
class Routine extends Eloquent {
public function emp()
{
return $this->belongsToMany('Emp');
}
}

在这种情况下,我们不需要 routines 中的外键字段 (emp_id)表,因为我们使用数据透视表来维护关系,但如果它在那里,它不会有问题。

应该注意的是,每当您在父表中插入/更新任何记录时,您还必须在数据透视表中插入/更新关系数据,Laravel 为这些提供了有用的方法,所以 check the manual (relationship) .

关于php - Laravel Eloquent 多对多模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22457015/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com