php在一个数据表里执行多个数据,PHP
我正在尝试在Laravel 5中创建一个应用程序,该程序可以跟踪患者的体检,以便创建患者历史记录,我使用Eloquent作为模型抽象建议.
为此,我使用laravel迁移创建了三个表和一个枢轴表,如下表所示:
用户可以是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不会显示该错误,或??者如果我在方法上出错了?任何帮助,我们将不胜感激.
php在一个数据表里执行多个数据,PHP
我正在尝试在Laravel 5中创建一个应用程序,该程序可以跟踪患者的体检,以便创建患者历史记录,我使用Eloquent作为模型抽象建议.
为此,我使用laravel迁移创建了三个表和一个枢轴表,如下表所示:
用户可以是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不会显示该错误,或??者如果我在方法上出错了?任何帮助,我们将不胜感激.