Php 在gridview Yii 2中显示另一个表的字段
我有一个名为hosts的表,另一个是hostgroup。在“我的主机”表中,它有以下字段:id、name和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
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()
函数添加到模型文件中