【发布时间】:2018-11-29 22:26:48
【问题描述】:
我在用户和论文之间有关系,我的 dataProvider 只在 Gridview 中显示用户属性而不是论文的属性。有没有比 ['attribute'] => ['table.attribute'] 更好的打印连接属性的方法?
型号:
public function search($params)
{
$query = Request::find()->joinWith('user')->joinWith('thesis')->where(['thesis.staff'=> Yii::$app->user->identity->id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
查看:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'id',
'title',
['attribute' => 'thesis',
'value' => 'thesis.title'],
//'company',
'description:ntext',
'duration',
['attribute' => 'user'
'value' => 'user.id'],
//'requirements',
//'course',
'n_person',
'ref_person',
'is_visible:boolean',
//'created_at',
//'staff',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
控制器:
public function actionIndex()
{
$searchModel = new SearchRequest();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
【问题讨论】:
-
不确定你在这里问什么,但你应该使用
->joinWith(['user', 'thesis th' => function (\yii\db\ActiveQuery $query) { $query->where(['th.staff'=> Yii::$app->user->identity->id]); } ]),你可以在一次调用joinWith()时将多个关系指定为数组。 -
还有,您在 gridview 中使用的是哪种搜索模型?
-
我的 $searchModel 来自上面的 SearchRequest->search() ,无论如何它仍然不打印论文属性。现在,我的关系是 User-Student-Thesis 与 User-Student (0-1,1-1) 和 Student-Thesis (N-N),它们创建了一个名为 Request 的新表/模型。我想问题可能出在 Request 模型中,因为 joinWith() 使用了那些 get 方法。
-
什么是请求模型?
标签: php gridview yii2 dataprovider