/** * Fill information about Farm/FarmRole for each object based on cloudServerId. * cloudServerId could be empty or didn't exist in our database. * * @param array[] $data Array of arrays */ private function applyFarmRoleInfo(&$data) { $cloudServerIds = []; foreach ($data as $row) { if ($row['cloudServerId']) { $cloudServerIds[] = $row['cloudServerId']; } } if (empty($cloudServerIds)) { return; } $server = new Entity\Server(); $history = new Entity\Server\History(); $farm = new Entity\Farm(); $farmRole = new Entity\FarmRole(); $cloudServerIds = join(",", array_map(function ($serverId) { return $this->db->qstr($serverId); }, $cloudServerIds)); $sql = "\n SELECT {$farm->columnId} AS farmId, {$farm->columnName} AS farmName, {$farmRole->columnId} AS farmRoleId,\n {$farmRole->columnAlias} AS farmRoleName, {$server->columnServerId} AS serverId, {$server->columnIndex} AS serverIndex,\n {$history->columnCloudServerId} AS cloudServerId FROM {$server->table()}\n JOIN {$history->table()} ON {$server->columnServerId} = {$history->columnServerId}\n JOIN {$farm->table()} ON {$server->columnFarmId} = {$farm->columnId}\n JOIN {$farmRole->table()} ON {$server->columnFarmRoleId} = {$farmRole->columnId}\n WHERE {$server->columnEnvId} = ? AND {$history->columnCloudServerId} IN ({$cloudServerIds})\n "; $result = []; foreach ($this->db->Execute($sql, [$this->getEnvironmentId()]) as $row) { $result[$row['cloudServerId']] = $row; } foreach ($data as &$row) { if (!empty($row['cloudServerId']) && !empty($result[$row['cloudServerId']])) { $row = array_merge($row, $result[$row['cloudServerId']]); } } }