您可以使用yii2中的listview显示相关表格中的数据
我表的型号代码是:
public static function tableName()
{
return 'quizselect';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['quiz_sel_cat', 'quiz_sel_type', 'quiz_sel_link'], 'required'],
[['quiz_sel_cat', 'quiz_sel_type'], 'integer'],
[['quiz_sel_link'], 'string', 'max' => 50],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'quiz_sel_id' => Yii::t('app', 'ID'),
'quiz_sel_cat' => Yii::t('app', 'Category'),
'catType.cat_label' => Yii::t('app', 'Category'),
'quiz_sel_type' => Yii::t('app', 'Type'),
'quiz_sel_link' => Yii::t('app', 'Link'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCatType()
{
return $this->hasOne(CatType::className(), ['cat_id' => 'quiz_sel_cat']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getQuizType()
{
return $this->hasOne(QuizType::className(), ['quiz_type_id' => 'quiz_sel_type']);
}
}
控制器代码:
ListView::widget([
'dataProvider' => $dataProviderQuizSelect,
'itemView' => '_post',
]);
查看代码:
<article class="item" data-key="<?= $model->quiz_sel_id; ?>">
<h2 class="title">
<?= Html::a(Html::encode($model->quiz_sel_cat), Url::toRoute(['site/index', 'id'=> $model->quiz_sel_id]),
['title' => $model->quiz_sel_cat, 'value'=>catType.cat_label]) ?>
</h2>
</article>
我知道你可以在GridView中使用:
[
'attribute'=>'quiz_sel_cat',
'value'=>'catType.cat_label',
],
但你可以在listview中做同样的事情,以便自定义数据显示。