我有一个名为hosts的表,另一个是hostgroup。在我的主机表上,它有以下字段:id、name和hostgroup_id
。在我的hostgroup
表中,它的字段也是id
和name
。我想在view/index.php
上显示来自主机的表,显示其id
、name
,以及来自主机组的表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',
],
?>