我有两个表:
因此,我想做的是,在该选项的视图中,显示该选项的视图,然后在下表中显示其所有活动(如果有活动)。我该怎么办?
示例(视图):
|选项:1 | '这是我的选择'|类型:1活动表(选项1):
| ----------------------------------------------- -------------- ||日期:2020-01-01 |管理员:1 |创建'这是我的选择|
|日期:2020-01-03 |管理员:1 |编辑'这是一个选项|
|日期:2020-02-10 |管理员:2 |编辑'这是选项|
| ----------------------------------------------- -------------- |
我能够显示每个活动,但只想显示该特定选项的活动。我无法按其ID进行过滤。
这是我的OptionsController
,actionView($ id):public function actionView($id)
{
$searchModel_act = new OptionsActivitySearch();
$dataProvider_act = $searchModel_act->search(Yii::$app->request->queryParams);
$model_activity_op = new OptionsActivity();
return $this->render('view', [
'searchModel_act' => $searchModel_act,
'dataProvider_act' => $dataProvider_act,
'model_activity_op' => $model_activity_op,
'model' => $this->findModel($id),
]);
}
(也尝试了其他尝试传递ID的选项,例如$ dataProvider_act = OpcionesActivity :: find()-> where([['id'=> $ id])-> orderBy(['fecha'=> SORT_ASC])-> all();。或['id'=> $ model-> id],但无效。:我的
视图
<?=GridView::widget([
'dataProvider' => $dataProvider_act,
'filterModel' => $searchModel_act,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'opcion',
'type',
'date',
'admin',
'action',
],
])
?>
在我的[[模型中,我也尝试过此操作,以访问活动但不起作用:
public function getActivity()
{
return $this->hasMany(OptionsActivity::class, ['id' => 'id']);
}
有什么想法吗?
提前感谢!
在控制器中:
public function actionView($id)
{
$searchModel = new OptionsActivitySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams, $id);
//---------------------------
return $this->render('view', [
'model' => $this->findModel($id),
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
在OptionsActivitySearch中:
public function search($params, $id)
{
$query = OpcionesActivity::find()->where(['id' => $id])->orderBy(['id' => SORT_DESC]);
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
//......
}
在视图中:
...
((gridView部分): <?= //$model->activity->opcion
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
//...
])
?>