Gibs
Gibs

Reputation: 774

Counting records from related tables Gridview

I have 3 tables named hostgroup, host and services.

I have a database like this for example.

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

The final table should be like this:

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

I was able to do the 2nd column which is Total Hosts by adding this function on my Hostgroup model:

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

How can I count the total warning and critical also?

EDIT: ADDED SOURCE CODE

HostgroupSearch model:

<?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;
    }
}

This HostlistController controls the display of everything in my table.

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

?>

This is then my view page 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',
        ],

Upvotes: 0

Views: 129

Answers (1)

Gibs
Gibs

Reputation: 774

Nevermind. Solved it by adding these functions to Host model.

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

and added this to Hostgroup model

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

modified index.php with this

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

Upvotes: 1

Related Questions