我正在使用Yii2 gridview小部件来显示数据.
我正在使用两个名为message和message_trigger的表.
在消息表中,列是object_model,Object_id.
在message_trigger中,该列是object_id,object_name.
网格从表消息中获取值.所以网格字段是Object_model,Object_id.
现在我的问题是我需要根据表消息中的object_id从表message_trigger中显示Object_name.
在我的表格中,我使用了这样的网格
<?= GridView::widget([ 'dataProvider' => $dataProvider,'filterModel' => $searchModel,'columns' => [ ['class' => 'yii\grid\SerialColumn'],'object_model','object_id',['class' => 'yii\grid\ActionColumn','template' => '{view} {update} {delete} '],],]); ?>
在我使用的模型中
public function search($params){ $query = AlertTrigger::find()->where(['alert_id'=>$params['id']])->andWhere(['!=','status',2]); $dataProvider = new ActiveDataProvider([ 'query' => $query,]); }
在消息模型中
public function getMessageTrigger() { return $this->hasOne(MessageTrigger::className(),['object_id' => 'object_id']); }
在视野中
<?= GridView::widget([ 'dataProvider' => $dataProvider,[ 'label' => 'Name','value' => 'messageTrigger.object_name',]); ?>