How to show yii2 database relation in view

て烟熏妆下的殇ゞ 提交于 2019-12-12 01:37:20

问题


I am new in yii2. I don't know much about table relation. I have a 3 tables shops promotions and promotion_details. promotion_details contains promotion id and shop id. promotion has status like active or inactive. i want display in shop view only active promotions. How can i Do that? Thanks in advance


回答1:


In you promotion_detail view add a getter function for relation and a getter function for the field

/* ActiveRelation */
public function getPropomition()
{
   return $this->hasOne(Promotion::className(), ['id' => 'promotion_id']);
}

/* Getter for status name */
public function getStatus() {
   return $this->prpmotion->status;
}

in view

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'id',
        .......
        'status',
        ['class' => 'yii\grid\ActionColumn'],
    ]
]);

this guide could be useful http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/



来源:https://stackoverflow.com/questions/38994984/how-to-show-yii2-database-relation-in-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!