I'm trying the following:
I have 3 models: Clinic
, Pack
and OfferedTreatment
.
Pack
and OfferedTreatment
are related to Clinic
by a FK clinic_id
and both models are related to each other by a junction model: Pack_OfferedTreatment
(pack_id
, offeredTreatment_id
). One Pack can have several OfferedTreatments
.
From ClinicController
I want a certain clinic and a certain pack related to that clinic and get all related offeredTreatments
to show a view
The result I have from the following function is it returns me ALL the offeredTreatments
associated to the clinic, but not to the pack....what am I doing wrong?
/**
* @param $clinicId
* @param $packId
* @return array
* @throws NotFoundHttpException
*/
public function actionPackOfferedTreatments($clinicId, $packId)
{
$clinicId = (int)$clinicId;
$packId = (int)$packId;
// Find model of the clinic
$model = $this->findModel($clinicId);
// pack offeredTreatments:
$packOfferedTreatmentDataProvider = new ActiveDataProvider([
'query' => $model->getOfferedTreatments()
->innerJoin('pack_offeredTreatment', false)
->where(['pack_offeredTreatment.pack_id' => $packId]),
'pagination' => false,
'sort' => [
'defaultOrder' => [
'order' => SORT_ASC
]
]
]);
Yii::$app->response->format = Response::FORMAT_JSON;
return $packOfferedTreatmentDataProvider->getModels();
}