Answer the question
In order to leave comments, you need to log in
How to properly get related data in Yii2?
In B.D. there are tables ` doctors ` and ` clinics `
Simplified: (I write only the fields that are needed for the Subject ...)
Table clinics: id , name (clinics)
Table doctors: id , name (doctors)
Table clinics_doctors: id , clinic_id , doctor_id (link: doctor/clinic)
One doctor can work in several clinics. There are connections. Yii2 Models: Clinics, Doctors, ClinicsDoctors
The task is to get a list of all doctors of a certain clinic...
How I do:
// Получаю массив ID всех врачей клиники (для примера, клиники id = 1)
$doctors_array = ClinicsDoctors::find()->select('doctor_id')->where(['clinic_id' => 1])->column();
// Получаю врачей из таблицы doctors по массиву ID
$doc_clinics = Doctors::find()->where(['IN', 'id', $doctors_array])->all();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question