Q: yii2 dataProvider 不显示连接属性
Q: yii2 dataProvider doesn't display join attributes
我在 User 和 Thesis 之间有关系,我的 dataProvider 只在 Gridview 中显示用户属性而不是 thesis'ones。有没有比 ['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,
]);
}
最好在 $this->load($params);
之后添加代码
我的例子是:
.
.
.
$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;
}
$query->joinWith('user');
$query->joinWith('thesis');
$query->andFilterWhere(['thesis.staff'=> Yii::$app->user->identity->id]);
.
.
.
您需要在模型中添加关系吸气剂。
在您的 Thesis 模型上,您需要添加类似
的内容
public function getUser0 () {
return $this->hasOne(\your\user\model\location\User::className(), ['id' => 'user']);
}
当您调用 Thesis::user0 时,在您的模型上使用它会通过延迟加载用户关系来填充,在您的情况下,是这样的:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
/// ---- other attributes
['attribute' => 'user0.name'],
/// ---- other attributes
],
]); ?>
我在 User 和 Thesis 之间有关系,我的 dataProvider 只在 Gridview 中显示用户属性而不是 thesis'ones。有没有比 ['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,
]);
}
最好在 $this->load($params);
之后添加代码我的例子是:
.
.
.
$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;
}
$query->joinWith('user');
$query->joinWith('thesis');
$query->andFilterWhere(['thesis.staff'=> Yii::$app->user->identity->id]);
.
.
.
您需要在模型中添加关系吸气剂。
在您的 Thesis 模型上,您需要添加类似
的内容public function getUser0 () {
return $this->hasOne(\your\user\model\location\User::className(), ['id' => 'user']);
}
当您调用 Thesis::user0 时,在您的模型上使用它会通过延迟加载用户关系来填充,在您的情况下,是这样的:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
/// ---- other attributes
['attribute' => 'user0.name'],
/// ---- other attributes
],
]); ?>