105 lines
2.5 KiB
Markdown
105 lines
2.5 KiB
Markdown
# 挂号管理联表查询修复
|
|
|
|
## 问题描述
|
|
|
|
挂号列表页面中,患者姓名和医生姓名没有显示,后端接口返回数据不全。
|
|
|
|
## 问题原因
|
|
|
|
在 `AppointmentLists.php` 和 `AppointmentLogic.php` 中,联表查询时使用的表名缺少表前缀 `la_`。
|
|
|
|
ThinkPHP 在使用 `leftJoin()` 时,需要指定完整的表名(包括表前缀),而不能只写表名。
|
|
|
|
## 错误代码
|
|
|
|
```php
|
|
// 错误:缺少表前缀
|
|
->leftJoin('user u', 'a.patient_id = u.id')
|
|
->leftJoin('admin ad', 'a.doctor_id = ad.id')
|
|
```
|
|
|
|
## 正确代码
|
|
|
|
```php
|
|
// 正确:包含表前缀 la_
|
|
->leftJoin('la_user u', 'a.patient_id = u.id')
|
|
->leftJoin('la_admin ad', 'a.doctor_id = ad.id')
|
|
```
|
|
|
|
## 修复的文件
|
|
|
|
### 1. server/app/adminapi/lists/doctor/AppointmentLists.php
|
|
|
|
修复了两处:
|
|
- `lists()` 方法中的联表查询
|
|
- `count()` 方法中的联表查询
|
|
|
|
### 2. server/app/adminapi/logic/doctor/AppointmentLogic.php
|
|
|
|
修复了一处:
|
|
- `detail()` 方法中的联表查询
|
|
|
|
## 数据库表说明
|
|
|
|
系统使用的表:
|
|
- `la_doctor_appointment` - 挂号表(主表)
|
|
- `la_user` - 用户表(患者信息)
|
|
- `la_admin` - 管理员表(医生信息)
|
|
|
|
字段映射:
|
|
- `u.nickname` → `patient_name` (患者姓名)
|
|
- `u.mobile` → `patient_phone` (患者电话)
|
|
- `ad.name` → `doctor_name` (医生姓名)
|
|
|
|
## 验证方法
|
|
|
|
修复后,访问挂号列表接口应该返回完整的数据:
|
|
|
|
```json
|
|
{
|
|
"code": 1,
|
|
"msg": "success",
|
|
"data": {
|
|
"lists": [
|
|
{
|
|
"id": 1,
|
|
"patient_id": 2,
|
|
"patient_name": "张三", // ✅ 现在有值了
|
|
"patient_phone": "13800138000", // ✅ 现在有值了
|
|
"doctor_id": 1,
|
|
"doctor_name": "李医生", // ✅ 现在有值了
|
|
"appointment_date": "2026-03-05",
|
|
"appointment_time": "09:00:00",
|
|
"status": 1,
|
|
"status_desc": "已预约",
|
|
...
|
|
}
|
|
]
|
|
}
|
|
}
|
|
```
|
|
|
|
## 注意事项
|
|
|
|
在 ThinkPHP 中使用联表查询时:
|
|
|
|
1. **使用模型的 leftJoin()**:必须指定完整表名(包括表前缀)
|
|
```php
|
|
->leftJoin('la_user u', 'a.patient_id = u.id')
|
|
```
|
|
|
|
2. **使用 Db::table()**:可以不加表前缀(框架会自动添加)
|
|
```php
|
|
Db::table('user')->alias('u')->...
|
|
```
|
|
|
|
3. **表前缀配置**:在 `config/database.php` 中配置
|
|
```php
|
|
'prefix' => 'la_',
|
|
```
|
|
|
|
## 相关文档
|
|
|
|
- `APPOINTMENT_MANAGEMENT_COMPLETE.md` - 挂号管理完整文档
|
|
- `APPOINTMENT_SETUP_GUIDE.md` - 配置指南
|