I have an object, Object1
, that references Object2
in a one-to-many relationship: one Object1
to many Object2
.
In my Object1
view, I am trying to include a yii\grid\GridView
of the related Object2
items only. Not in addition to the Object1
data. Just a GridView
of Object2
items only.
Using code like that below, I believe I have to set both $dataProvider
and $searchModel
in my controller but I'm not sure how to link Yii::$app->request->queryParams
with $id
.
My code just returns all Object2
items, regardless of their relationship with Object1
, which although makes perfect sense to me, is not what I'm looking for.
I am not even sure that this is the correct approach. Does anyone know of a solution? Thanks in advance.
/* Object1 model */
public function getRelations() {
return $this->hasMany(Object2::className(), ['relation' => 'id']);
}
/* Object1 view */
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'attr1',
'attr2',
'attr3',
'attr4',
['class' => 'yii\grid\ActionColumn'],
],
]);
/* Object1 controller */
public function actionView($id){
$searchModel = new Object2Search();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('view', [
'model' => $this->findModel($id),
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}