在网格视图Yii 2中显示另一个表的字段2


Displaying field of another table in a gridview Yii 2

我有一个名为hosts的表,另一个是hostgroup。在我的主机表上,它有以下字段:id、name和hostgroup_id。在我的hostgroup表中,它的字段也是idname。我想在view/index.php上显示来自主机的表,显示其idname,以及来自主机组的表hostgroup.name,根据其id Also, do you have any idea how I can make it redirect to the是否更新该特定id的page of主机组?

在我的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',
        ],
    ?>  

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',
    ],
?>