2015-12-15 41 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', 
    ], 
?> 
相關問題