@@ -36,8 +36,11 @@ class VolatileStateResults extends ResultSet
3636 /** @var string Object type service */
3737 protected const TYPE_SERVICE = 'service ' ;
3838
39- /** @var array Columns to be selected if they were explicitly set, if empty all columns are selected */
40- protected array $ columns ;
39+ /** @var array|null Columns to be selected if they were explicitly set, if empty all columns are selected */
40+ protected ?array $ columns ;
41+
42+ /** @var bool Whether the model's ID should be contained in the results */
43+ protected bool $ includeModelID = true ;
4144
4245 public static function fromQuery (Query $ query )
4346 {
@@ -46,6 +49,20 @@ public static function fromQuery(Query $query)
4649 $ self ->redisUnavailable = Backend::getRedis ()->isUnavailable ();
4750 $ self ->columns = $ query ->getColumns ();
4851
52+ if (! empty ($ self ->columns )) {
53+ // The id is necessary to apply the redis-updates
54+ if ($ query ->getModel () instanceof Host && empty (array_intersect (['host.id ' , 'id ' ], $ self ->columns ))) {
55+ $ query ->withColumns ('host.id ' );
56+ $ self ->includeModelID = false ;
57+ } elseif (
58+ $ query ->getModel () instanceof Service &&
59+ empty (array_intersect (['service.id ' , 'id ' ], $ self ->columns ))
60+ ) {
61+ $ query ->withColumns ('service.id ' );
62+ $ self ->includeModelID = false ;
63+ }
64+ }
65+
4966 return $ self ;
5067 }
5168
@@ -66,7 +83,12 @@ public function current()
6683 $ this ->rewind ();
6784 }
6885
69- return parent ::current ();
86+ $ result = parent ::current ();
87+ if (! $ this ->includeModelID ) {
88+ unset($ result ['id ' ]);
89+ }
90+
91+ return $ result ;
7092 }
7193
7294 public function next (): void
0 commit comments