在 Yii2 上使用 ActiveDataProvider innerJoin
innerJoin with ActiveDataProvider on Yii2
我正在尝试以下操作:
我有 3 个模型:Clinic
、Pack
和 OfferedTreatment
。
Pack
和 OfferedTreatment
通过 FK clinic_id
与 Clinic
相关,并且两个模型通过连接模型相互关联:Pack_OfferedTreatment
(pack_id
、offeredTreatment_id
)。一包可以有几个OfferedTreatments
.
来自ClinicController
我想要某个诊所和与该诊所相关的某个包并获取所有相关offeredTreatments
以显示视图
我从以下函数得到的结果是 returns 我所有与诊所相关的 offeredTreatments
,但与包无关....我做错了什么?
/**
* @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();
}
我会换个方式考虑。您说 Offered Treatment 与具有外键的诊所相关 - 但它不应该。 pack与clinic相关,治疗与pack相关,所以已经是trough pack相关了。
如果您建立了关系,Clinic has Many Packs,并且 Packs hasMany treatments,您可以 运行 查询 ->innerJoinWith('packs.treatments')
的诊所。如果你有包,你可以 运行 ->with(['clinic', 'treatments'])
.
来自docs:
You can eagerly load deeply nested relations, such as a.b.c.d
. All parent relations will be eagerly loaded. That is, when you call with() using a.b.c.d
, you will eagerly load a
, a.b
, a.b.c
and a.b.c.d
.
我正在尝试以下操作:
我有 3 个模型:Clinic
、Pack
和 OfferedTreatment
。
Pack
和 OfferedTreatment
通过 FK clinic_id
与 Clinic
相关,并且两个模型通过连接模型相互关联:Pack_OfferedTreatment
(pack_id
、offeredTreatment_id
)。一包可以有几个OfferedTreatments
.
来自ClinicController
我想要某个诊所和与该诊所相关的某个包并获取所有相关offeredTreatments
以显示视图
我从以下函数得到的结果是 returns 我所有与诊所相关的 offeredTreatments
,但与包无关....我做错了什么?
/**
* @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();
}
我会换个方式考虑。您说 Offered Treatment 与具有外键的诊所相关 - 但它不应该。 pack与clinic相关,治疗与pack相关,所以已经是trough pack相关了。
如果您建立了关系,Clinic has Many Packs,并且 Packs hasMany treatments,您可以 运行 查询 ->innerJoinWith('packs.treatments')
的诊所。如果你有包,你可以 运行 ->with(['clinic', 'treatments'])
.
来自docs:
You can eagerly load deeply nested relations, such as
a.b.c.d
. All parent relations will be eagerly loaded. That is, when you call with() usinga.b.c.d
, you will eagerly loada
,a.b
,a.b.c
anda.b.c.d
.