/** * Searches servers by criteria and selecting and initiating their Properties * * @param array $criteria optional The search criteria. * @param array $group optional The group by looks like [property1, ...], by default groups by `serverId` * @param array $order optional The results order looks like [property1 => true|false, ... ] * @param int $limit optional The records limit * @param int $offset optional The offset * @param bool $countRecords optional True to calculate total number of the records without limit * * @return ArrayCollection|Server[] Returns collection of the entities. * * @throws \Scalr\Exception\ModelException */ public static function findWithProperties(array $criteria = null, array $group = null, array $order = null, $limit = null, $offset = null, $countRecords = null) { $server = new Server(); /* @var $servers Server[] */ $servers = []; if (!isset($group)) { $group = ['serverId']; } $collection = $server->result(AbstractEntity::RESULT_ENTITY_COLLECTION)->find($criteria, $group, $order, $limit, $offset, $countRecords); foreach ($collection as $server) { /* @var $server Server */ $servers[$server->serverId] = $server; } if (count($servers) > 0) { $propertiesCollection = ServerProperty::find([['serverId' => ['$in' => array_keys($servers)]], ['$or' => [['name' => Scalr_Role_Behavior::SERVER_BASE_HOSTNAME], ['name' => self::LAUNCH_REASON], ['name' => self::LAUNCHED_BY_ID]]]]); /* @var $serverProperties ServerProperty[] */ $serverProperties = []; foreach ($propertiesCollection as $property) { /* @var $property ServerProperty */ $serverProperties[$property->serverId][$property->name] = $property; } foreach ($servers as $serverId => $server) { if (isset($serverProperties[$serverId][Scalr_Role_Behavior::SERVER_BASE_HOSTNAME])) { $servers[$serverId]->properties[Scalr_Role_Behavior::SERVER_BASE_HOSTNAME] = $serverProperties[$serverId][Scalr_Role_Behavior::SERVER_BASE_HOSTNAME]->value; } else { $servers[$serverId]->properties[Scalr_Role_Behavior::SERVER_BASE_HOSTNAME] = null; } if (isset($serverProperties[$serverId][self::LAUNCH_REASON])) { $servers[$serverId]->properties[self::LAUNCH_REASON] = $serverProperties[$serverId][self::LAUNCH_REASON]->value; } else { $servers[$serverId]->properties[self::LAUNCH_REASON] = null; } if (isset($serverProperties[$serverId][self::LAUNCHED_BY_ID])) { $servers[$serverId]->properties[self::LAUNCHED_BY_ID] = $serverProperties[$serverId][self::LAUNCHED_BY_ID]->value; } else { $servers[$serverId]->properties[self::LAUNCHED_BY_ID] = null; } } } return $collection; }