gpt4 book ai didi

php - 统计相关表Gridview中的记录

转载 作者:行者123 更新时间:2023-11-29 11:49:25 25 4
gpt4 key购买 nike

我有 3 个表,分别名为 hostgrouphostservices

例如,我有一个这样的数据库。

Hostgroup: 
id, name
1 GroupA
2 GroupB

Host:
id, name, hostgroup_id
1 HostA 1
2 HostB 1
3 HostC 2
4 HostD 2
5 HostE 2


//2 = critical, 1 = warning
Service:
id, host_id, state
1 1 2
2 1 2
3 2 1
4 2 1
5 3 2

决赛 table 应该是这样的:

Name        Total Hosts          Total Critical       Total Warning
GroupA 2 2 2
GroupB 3 1 0

通过在我的 Hostgroup 模型上添加此函数,我能够完成第二列,即 Total Hosts:

public function getHostcount()
{
return Host::find()->where(['hostgroup_id' => $this->id])->count();
}

如何计算警告和严重的总数?

  • 总计严重(统计与主机组相关的主机总计处于严重状态的数量)
  • 警告总数(统计与主机组相关的主机的警告状态总数)

编辑:添加源代码

HostgroupSearch 模型:

<?php
class HostgroupSearch extends Hostgroup
{
public function rules()
{
return [
[['id', 'check_interval', 'retry_interval', 'max_check_attempts', 'timeout', 'freshness_threshold', 'is_auto_recovery', 'reflect_flg', 'created_user_id'], 'integer'],
[['object_name', 'name', 'name_search', 'remarks', 'snmp_community', 'timestamp'], 'safe'],
];
}

public function search($params)
{
$query = Hostgroup::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);

$this->load($params);

if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
return $dataProvider;
}

$query->andFilterWhere([
'id' => $this->id,
'check_interval' => $this->check_interval,
'retry_interval' => $this->retry_interval,
'max_check_attempts' => $this->max_check_attempts,
'timeout' => $this->timeout,
'freshness_threshold' => $this->freshness_threshold,
'is_auto_recovery' => $this->is_auto_recovery,
'reflect_flg' => $this->reflect_flg,
'created_user_id' => $this->created_user_id,
'timestamp' => $this->timestamp,
]);

$query->andFilterWhere(['like', 'object_name', $this->object_name])
->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'name_search', $this->name_search])
->andFilterWhere(['like', 'remarks', $this->remarks])
->andFilterWhere(['like', 'snmp_community', $this->snmp_community]);

return $dataProvider;
}
}

这个HoSTListController控制我表格中所有内容的显示。

<?
class HostlistController extends Controller
{
public $layout = "default";

public function actionIndex()
{
$searchModel = new HostgroupSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
}

?>

这就是我的 View 页面index.php:

'columns' => [
[
'label' => 'Name',
'attribute' => 'name',
'format' => 'raw',
'value' => function ($data, $id) {
return Html::a($data->name, '/index.php/hostlistbygroup/'. $id);
},
],
[
'label' => 'Total Host',
'attribute' => 'hostcount',
],
[
'label' => 'Total Critical',
'attribute' => 'host.criticalcount',
],
[
'label' => 'Total Warning',
'attribute' => 'host.warningcount',
],

最佳答案

没关系。通过将这些函数添加到 Host 模型中解决了这个问题。

public function getCriticalcount()
{
return ServiceStatuses::find()->where(['host_id' => $this->id, 'last_hard_state' => '2'])->count();
}

public function getWarningcount()
{
return ServiceStatuses::find()->where(['host_id' => $this->id, 'last_hard_state' => '1'])->count();
}

并将其添加到Hostgroup模型

public function getHost()
{
return $this->hasOne(Host::className(), ['hostgroup_id' => 'id']);
}

用这个修改了index.php

 'columns' => [
[
'label' => 'Name',
'attribute' => 'name',
'format' => 'raw',
'value' => function ($data, $id) {
return Html::a($data->name, '/index.php/hostlistbygroup/'. $id);
},
],
[
'label' => 'Total Host',
'attribute' => 'hostcount',
],
[
'label' => 'Total Critical',
'attribute' => 'host.criticalcount',
],
[
'label' => 'Total Warning',
'attribute' => 'host.warningcount',
],

关于php - 统计相关表Gridview中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34388791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com