我正在尝试在Laravel 5中创建一个应用程序,该程序可以跟踪患者的体检,以便创建患者历史记录,我使用Eloquent作为模型抽象建议.

为此,我使用laravel迁移创建了三个表和一个枢轴表,如下表所示:

如何将多个数据表的字段作为一个java实体类的映射表_App

用户可以是Doctor或Administrators,他们输入他们进行的特定考试的患者分数结果,例如,我有一个数据透视表,其中包含三个与三个实体相关的外键:

>考试编号

> Patient_id
> user_id

在Eloquent中,我根据Laravel文档Many To Many建立了这些多对多模型,以支持建议的业务逻辑:

// Patient.php
class Patient extends Model
{
public function exams() {
return $this->belongsToMany('App\Exam', 'exam_patient');
}
}
// Exam.php
class Exam extends Model
{
public function patients()
{
return $this->belongsToMany('App\Patient', 'exam_patient');
}
public function users()
{
return $this->belongstoMany('App\User', 'exam_patient');
}
}
// User.php
class User extends Model
{
public function exams() {
return $this->belongsToMany('App\Exam', 'exam_patient');
}
}

在修补匠中,我尝试在进行重大更改之前模拟一个常见用例:医生对病人进行检查:

>>> $user= App\User::first()
=> App\User {#671
id: "1",
name: "Victor",
email: "",
created_at: "2015-10-10 18:33:54",
updated_at: "2015-10-10 18:33:54",
}
>>> $patient= App\Patient::first()
=> App\Patient {#669
id: "1",
username: "",
name: "Tony",
lastname: "",
birthday: "0000-00-00",
created_at: "2015-10-10 18:32:56",
updated_at: "2015-10-10 18:32:56",
}
>>> $exam= App\Exam::first()
=> App\Exam {#680
id: "1",
name: "das28",
title: "Das28",
created_at: "2015-10-10 18:31:31",
updated_at: "2015-10-10 18:31:31",
}
>>> $user->save()
=> true
>>> $patient->save()
=> true
>>> $exam->save()
=> true

问题是,当我尝试链接(或附加)至少两个模型时,出现以下错误:

>>> $patient->exams()->attach(1)
Illuminate\Database\QueryException with message
'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a
child row: a foreign key constraint fails (`cliniapp`.`exam_patient`, CONSTRAINT
`exam_patient_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
ON DELETE CASCADE) (SQL: insert into `exam_patient` (`exam_id`, `patient_id`)
values (1, 1))'

如果我什至连不上两个模型,该如何链接这三个模型?修补匠是否可以通过任何方式大规模附加它们,以便可以将它们关联到我的数据透视表(exam_ Patient)中,而MySql不会显示该错误,或者如果我在方法上出错了?任何帮助,我们将不胜感激.

解决方法:

如果您的Many to Many关系中有其他字段,则在声明该关系时必须“注册”它们,如上面链接的“检索中间表列”部分所示.

因此,您应始终声明“额外”数据透视表,这些数据透视表取决于定义关系的位置而有所不同:

class Patient extends Model
{
public function exams()
{
return $this->belongsToMany('App\Exam', 'exam_patient')->withPivot('user_id');
}
}
class Exam extends Model
{
public function patients()
{
return $this->belongsToMany('App\Patient', 'exam_patient')->withPivot('user_id');
}
public function users()
{
return $this->belongstoMany('App\User', 'exam_patient')->withPivot('patient_id');
}
}
class User extends Model
{
public function exams()
{
return $this->belongsToMany('App\Exam', 'exam_patient')->withPivot('patient_id');
}
}

在“附加/分离”部分中,您可以看到可以像这样设置这些额外的字段:

$patient->exams()->attach($exam->id, ['user_id' => $user->id]);

您的问题是,当您在不传递user_id列数据的情况下创建患者/检查关系时,exam_病人表上的user_id外键约束失败.

如果您出于某种原因不想总是这样传递user_id,也可以使该user_id列在数据库中为空.

当然,可以争论的是,使用两个数据透视表(exam_patient和exam_user)或使用多对多多态关系将是一种更好的方法.

从Laravel docs on polymorphic relations中可以看到,要在此处实现它们,我们将exam_ Patient表删除为可检查表,该表将包含3列:exam_id,examable_id和examable_type.

模型看起来像:

class Patient extends Model
{
public function exams()
{
return $this->morphToMany('App\Exam', 'examable');
}
}
class Exam extends Model
{
public function patients()
{
return $this->morphedByMany('App\Patient', 'examable');
}
public function users()
{
return $this->morphedByMany('App\User', 'examable');
}
}
class User extends Model
{
public function exams()
{
return $this->morphToMany('App\Exam', 'examable');
}
}

要完全更新可检查表,您可以执行以下操作:

$exam->patients()->save($patient);
$exam->users()->save($user);

因此,这是两个查询而不是一个,您失去了外键关系,并且“ examables”是一个尴尬的名字.

争论的理由是,这种关系更具声明性,并且更容易为属于多个用户/医生的单个患者进行一次检查.

标签:laravel-5,eloquent,pivot-table,mysql,php