Php 在gridview Yii 2中显示另一个表的字段

Php 在gridview Yii 2中显示另一个表的字段,php,gridview,yii2,Php,Gridview,Yii2,我有一个名为hosts的表,另一个是hostgroup。在“我的主机”表中,它有以下字段:id、name和hostgroup\u id。在我的hostgroup表中,其字段也是id和name。我想在视图/index.php主机表中显示它的id,name,并根据它的id从主机组表hostgroup.name。 另外,您知道我如何将其重定向到该id的主机组的update页面吗 在我的main_目录/model/HostsSearch.php中,我有以下内容: public function get

我有一个名为hosts的表,另一个是hostgroup。在“我的主机”表中,它有以下字段:id、name和
hostgroup\u id
。在我的
hostgroup
表中,其字段也是
id
name
。我想在
视图/index.php
主机表中显示它的
id
name
,并根据它的
id
从主机组表
hostgroup.name

另外,您知道我如何将其重定向到该id的主机组的
update
页面吗

在我的
main_目录/model/HostsSearch.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' => 'HostgroupsSearch.name',
            ],
            'ip_address',
            'object_name',
        ],
    ?>  
在我的
main_dir/view/hosts/index.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' => 'HostgroupsSearch.name',
            ],
            'ip_address',
            'object_name',
        ],
    ?>  

主目录/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',
    ],
?> 
将此
getHostgroup()
函数添加到模型文件中