如何在视图中显示yii2数据库关系 [英] How to show yii2 database relation in view

查看:175
本文介绍了如何在视图中显示yii2数据库关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是yii2的新手.我对表关系了解不多.我有一个3桌店的促销和Promotion_details. Promotion_details包含促销ID和商店ID.促销状态为有效或无效.我想在商店视图中仅显示活动促销.我怎样才能做到这一点?预先感谢

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

推荐答案

在promotion_detail视图中,添加用于关系的getter函数和用于字段的getter函数

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

查看中

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

该指南可能很有用 http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/

这篇关于如何在视图中显示yii2数据库关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆