DBRole::loadByFilter PHP Method

loadByFilter() public static method

public static loadByFilter ( array $filter )
$filter array
    public static function loadByFilter(array $filter)
    {
        $db = \Scalr::getDb();
        $sql = "SELECT id FROM roles WHERE 1=1";
        $args = array();
        foreach ($filter as $k => $v) {
            $sql .= " AND `{$k}`=?";
            $args[] = $v;
        }
        $roles = $db->GetAll($sql, $args);
        if (count($roles) == 1) {
            return self::loadById($roles[0]['id']);
        } else {
            $retval = array();
            foreach ($roles as $role) {
                $retval[] = self::loadById($role['id']);
            }
            return $retval;
        }
    }

Usage Example

 public function ServerImageCreate($ServerID, $RoleName)
 {
     $this->restrictAccess(Acl::RESOURCE_FARMS_ROLES, Acl::PERM_FARMS_ROLES_CREATE);
     $DBServer = DBServer::LoadByID($ServerID);
     // Validate client and server
     if ($DBServer->envId != $this->Environment->id) {
         throw new Exception(sprintf("Server ID #%s not found", $ServerID));
     }
     $this->user->getPermissions()->validate($DBServer);
     //Check for already running bundle on selected instance
     $chk = $this->DB->GetOne("SELECT id FROM bundle_tasks WHERE server_id=? AND status NOT IN ('success', 'failed') LIMIT 1", array($ServerID));
     if ($chk) {
         throw new Exception(sprintf(_("Server '%s' is already synchonizing."), $ServerID));
     }
     //Check is role already synchronizing...
     $chk = $this->DB->GetOne("SELECT server_id FROM bundle_tasks WHERE prototype_role_id=? AND status NOT IN ('success', 'failed') LIMIT 1", array($DBServer->GetFarmRoleObject()->RoleID));
     if ($chk && $chk != $DBServer->serverId) {
         try {
             $bDBServer = DBServer::LoadByID($chk);
             if ($bDBServer->farmId == $DBServer->farmId) {
                 throw new Exception(sprintf(_("Role '%s' is already synchonizing."), $DBServer->GetFarmRoleObject()->GetRoleObject()->name));
             }
         } catch (Exception $e) {
         }
     }
     try {
         $DBRole = DBRole::loadByFilter(array("name" => $RoleName, "env_id" => $DBServer->envId));
     } catch (Exception $e) {
     }
     if (!$DBRole) {
         $ServerSnapshotCreateInfo = new ServerSnapshotCreateInfo($DBServer, $RoleName, SERVER_REPLACEMENT_TYPE::NO_REPLACE, BundleTask::BUNDLETASK_OBJECT_ROLE, 'Bundled via API');
         $BundleTask = BundleTask::Create($ServerSnapshotCreateInfo);
         $BundleTask->createdById = $this->user->id;
         $BundleTask->createdByEmail = $this->user->getEmail();
         $BundleTask->save();
         $response = $this->CreateInitialResponse();
         $response->BundleTaskID = $BundleTask->id;
         return $response;
     } else {
         throw new Exception(_("Specified role name is already used by another role"));
     }
 }
All Usage Examples Of DBRole::loadByFilter