|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* DeviceGroup.php |
|
4
|
|
|
* |
|
5
|
|
|
* Dynamic groups of devices |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU General Public License as published by |
|
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
* @package LibreNMS |
|
21
|
|
|
* @link http://librenms.org |
|
22
|
|
|
* @copyright 2016 Tony Murray |
|
23
|
|
|
* @author Tony Murray <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace App\Models; |
|
27
|
|
|
|
|
28
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* App\Models\DeviceGroup |
|
32
|
|
|
* |
|
33
|
|
|
* @property integer $id |
|
34
|
|
|
* @property string $name |
|
35
|
|
|
* @property string $desc |
|
36
|
|
|
* @property string $pattern |
|
37
|
|
|
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Device[] $devices |
|
38
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\DeviceGroup whereId($value) |
|
39
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\DeviceGroup whereName($value) |
|
40
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\DeviceGroup whereDesc($value) |
|
41
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\DeviceGroup wherePattern($value) |
|
42
|
|
|
* @mixin \Eloquent |
|
43
|
|
|
*/ |
|
44
|
|
|
class DeviceGroup extends Model |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* Indicates if the model should be timestamped. |
|
48
|
|
|
* |
|
49
|
|
|
* @var bool |
|
50
|
|
|
*/ |
|
51
|
|
|
public $timestamps = false; |
|
52
|
|
|
/** |
|
53
|
|
|
* The table associated with the model. |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $table = 'device_groups'; |
|
58
|
|
|
/** |
|
59
|
|
|
* The primary key column name. |
|
60
|
|
|
* |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $primaryKey = 'id'; |
|
64
|
|
|
/** |
|
65
|
|
|
* Virtual attributes |
|
66
|
|
|
* |
|
67
|
|
|
* @var string |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $appends = ['deviceCount']; |
|
70
|
|
|
|
|
71
|
|
|
protected $fillable = ['name', 'desc', 'pattern']; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Fetch the device counts for groups |
|
75
|
|
|
* Use DeviceGroups::with('deviceCountRelation') to eager load |
|
76
|
|
|
* |
|
77
|
|
|
* @return int |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getDeviceCountAttribute() |
|
80
|
|
|
{ |
|
81
|
|
|
// if relation is not loaded already, let's do it first |
|
82
|
|
|
if (!$this->relationLoaded('deviceCountRelation')) { |
|
83
|
|
|
$this->load('deviceCountRelation'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$related = $this->getRelation('deviceCountRelation')->first(); |
|
87
|
|
|
|
|
88
|
|
|
// then return the count directly |
|
89
|
|
|
return ($related) ? (int)$related->count : 0; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
// ---- Define Relationships ---- |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Relationship to App\Models\Device |
|
97
|
|
|
* |
|
98
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
99
|
|
|
*/ |
|
100
|
|
|
public function devices() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->belongsToMany('App\Models\Device', 'device_group_device', 'device_group_id', 'device_id'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Relationship allows us to eager load device counts |
|
107
|
|
|
* DeviceGroups::with('deviceCountRelation') |
|
108
|
|
|
* |
|
109
|
|
|
* @return mixed |
|
110
|
|
|
*/ |
|
111
|
|
|
public function deviceCountRelation() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->devices()->selectRaw('`device_group_device`.`device_group_id`, count(*) as count')->groupBy('pivot_device_group_id'); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|