【问题标题】:Counting records from related tables Gridview统计相关表Gridview中的记录
【发布时间】:2015-12-21 04:03:04
【问题描述】:

我有 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

决赛桌应该是这样的:

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();
}

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

  • Total critical(统计与主机组相关的主机的关键状态总数)
  • 总警告(统计与主机组相关的主机的总警告状态数)

编辑:添加源代码

主机组搜索模型:

<?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,
        ]);
    }
}

?>

这就是我的查看页面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',
        ],

【问题讨论】:

  • 我想这就是你需要的:yiiframework.com/wiki/621/…
  • 您好,感谢您的评论。我已经尝试过在我的其他网格视图上实际使用它,比如在我的主机的视图页面中,而不是 hostgroup_id 我显示了它的名称。现在我的问题是关于计算。
  • 请添加代码、模板的sn-p和activerecord调用
  • 我不明白,你要添加三列还是三个字段?。您尝试查看的结果是数量,无法理解为什么它们在 gridview 的列中。解释得更好。
  • 嗨@scaisEdge 我添加了其他信息。我希望现在一切都清楚了。

标签: php mysql gridview yii2


【解决方案1】:

没关系。通过将这些函数添加到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',
        ],

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多