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/