2015-12-15 42 views
1

我有一个名为hosts的表,另一个表是hostgroup。在我的主机桌上它有这些字段:ID,名称和hostgroup_id。在我的hostgroup表中,它的字段也是idname。我想从主机表中显示view/index.php,显示其id,name和根据其id从主机组表hostgroup.name Also, do you have any idea how I can make it redirect to the更新page of hostgroup`那个特定的ID?在GridView中显示另一个表的字段Yii 2

在我main_directory/model/HostsSearch.php我有这样的:

public function getHostgroup() 
{ 
    return $this->hasOne(HostgroupsSearch::className(), ['id' => 'parent_host_id']); 
} 

虽然在我main_dir/view/hosts/index.php我有这样的:

<?= 
    GridView::widget([ 
     'dataProvider' => $dataProvider, 
     'filterModel' => $searchModel, 
     'columns' => [ 
      'id', 
      'name', 
      'parent_host_id.name', 
      [ 
       'label' => 'Name', 
       'value' => 'HostgroupsSearch.name', 
      ], 
      'ip_address', 
      'object_name', 
     ], 
    ?> 
+0

将此'getHostgroup()'函数添加到您的模型文件 – vishuB

回答

2

添加此功能在您的main_directory/model/Hosts.php模型文件

public function getHostgroup() 
{ 
    return $this->hasOne(HostgroupsSearch::className(), ['id' => 'parent_host_id']); 
} 

网查看:

<?= GridView::widget([ 
    'dataProvider' => $dataProvider, 
    'filterModel' => $searchModel, 
    'columns' => [ 
     'id', 
     'name', 
     'parent_host_id.name', 
     [ 
      'label' => 'Name', 
      'value' => 'hostgroup.name', 
     ], 
     'ip_address', 
     'object_name', 
    ], 
?> 
相关问题