Моя структура базы данных
Таблица отделов содержит
Таблица врача содержит
Doctor.php//Название модели
public function fields() {
return [
'doctor_id',
'd_name',
'email',
'phone',
'speciality',
'department' => function ($model) {
return $model->department->department; // Return related model property, correct according to your structure
},
'qualification',
'profile_image',
'experience',
];
}
web.php
[
'class' => 'yii\rest\UrlRule',
'controller' => ['doctor','department'],
],
Данные JSON
[
{
"doctor_id":1,
"d_name":"Saurabh Joshi",
"email":"****@gmail.com",
"phone":0,
"speciality":"1",
"department_id":"1",
"qualification":"M.S. (General Surgery) – Marathwada University\n MBBS – Marathwada University",
"profile_image":"Saurabh_Joshi1-1439384488.jpg",
"experience":""
}
]
Я хочу, чтобы department name
вместо department_id
когда я извлекал данные из таблицы врачей, чтобы показать их в форме.
Я не уверен в этом, но вы можете попробовать.
В модели Doctor.php
public function getDepartment()
{
return $this->hasOne(Department::className(), ['department_id' => 'department_id']);
}
и в fields()
public function fields() {
return [
'doctor_id',
'd_name',
'email',
'phone',
'speciality',
'department' => 'department.department',
//or
'Department' => 'department' => function () {
return $this->getDepartment->department;
},
'qualification',
'profile_image',
'experience',
];
}