0
votes

Can you Display data from related table using listview in yii2

The model code to my table is:

 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']);
    }

    }

controller code:

ListView::widget([
    'dataProvider' => $dataProviderQuizSelect,
    'itemView' => '_post',
]);   

view code:

<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>

I know you can do in in GridView using:

        [
            'attribute'=>'quiz_sel_cat',
            'value'=>'catType.cat_label',
        ],

but can you do the same in listview so customized my display of the data.

1
show your related model code please ..ScaisEdge
what do you mean by related model code?samauto
I mean show the model code for the table/model involved, a brief explaination the relation between these , the action code for this view and the view code ..ScaisEdge

1 Answers

1
votes

May be this link will be helpful for you Read here