DBRole::save PHP Method

save() public method

public save ( )
    public function save()
    {
        if (!$this->id) {
            $this->db->Execute("INSERT INTO roles SET\n                name\t\t= ?,\n                dtadded     = NOW(),\n                description\t= ?,\n                generation\t= ?,\n                origin\t\t= ?,\n                env_id\t\t= ?,\n                cat_id      = ?,\n                client_id\t= ?,\n                behaviors\t= ?,\n\n                added_by_userid = ?,\n                added_by_email  = ?,\n\n                os_id\t\t    = ?,\n                is_scalarized\t= ?\n            ", array($this->name, $this->description, $this->generation, $this->origin, empty($this->envId) ? null : $this->envId, $this->catId, empty($this->clientId) ? null : $this->clientId, $this->behaviorsRaw, $this->addedByUserId, $this->addedByEmail, $this->osId, $this->isScalarized));
            $this->id = $this->db->Insert_ID();
            $this->db->Execute("DELETE FROM role_behaviors WHERE role_id = ?", array($this->id));
            foreach ($this->getBehaviors() as $behavior) {
                $this->db->Execute("INSERT IGNORE INTO role_behaviors SET role_id = ?, behavior = ?", array($this->id, $behavior));
            }
        } else {
            $this->db->Execute("\n                UPDATE roles SET\n                    name\t\t= ?,\n                    description\t= ?,\n                    behaviors\t= ?,\n                    dt_last_used = ?,\n                    is_quick_start = ?,\n                    is_deprecated = ?\n                WHERE id =?\n            ", array($this->name, $this->description, $this->behaviorsRaw, $this->dtLastUsed, $this->isQuickStart, $this->isDeprecated, $this->id));
            $this->db->Execute("DELETE FROM role_behaviors WHERE role_id = ?", array($this->id));
            foreach ($this->getBehaviors() as $behavior) {
                $this->db->Execute("INSERT IGNORE INTO role_behaviors SET role_id = ?, behavior = ?", array($this->id, $behavior));
            }
        }
        $this->syncAnalyticsTags();
        return $this;
    }

Usage Example

示例#1
0
文件: Roles.php 项目: recipe/scalr
 public function xSave2Action()
 {
     $this->request->restrictAccess(Acl::RESOURCE_FARMS_ROLES, Acl::PERM_FARMS_ROLES_MANAGE);
     $this->request->defineParams(array('roleId' => array('type' => 'int'), 'behaviors' => array('type' => 'array'), 'tags' => array('type' => 'array'), 'description', 'name', 'os', 'parameters' => array('type' => 'json'), 'removedImages' => array('type' => 'json'), 'images' => array('type' => 'json'), 'properties' => array('type' => 'json'), 'scripts' => array('type' => 'json'), 'chef' => array('type' => 'json')));
     $id = $this->getParam('roleId');
     if ($id == 0) {
         if ($this->user->isScalrAdmin()) {
             $origin = ROLE_TYPE::SHARED;
             $envId = 0;
             $clientId = 0;
         } else {
             $origin = ROLE_TYPE::CUSTOM;
             $envId = $this->environment->id;
             $clientId = $this->user->getAccountId();
         }
         $dbRole = new DBRole(0);
         $dbRole->generation = 2;
         $dbRole->origin = $origin;
         $dbRole->envId = $envId;
         $dbRole->clientId = $clientId;
         $dbRole->catId = $this->getParam('catId');
         $dbRole->name = $this->getParam('name');
         $dbRole->os = $this->getParam('os');
         $dbRole->osGeneration = $this->getParam('osGeneration');
         $dbRole->osFamily = $this->getParam('osFamily');
         $dbRole->osVersion = $this->getParam('osVersion');
         $dbRole->addedByEmail = $this->user->getEmail();
         $dbRole->addedByUserId = $this->user->getId();
         $dbRole->save();
         $dbRole->setBehaviors(array_values($this->getParam('behaviors')));
     } else {
         $dbRole = DBRole::loadById($id);
         if (!$this->user->isScalrAdmin()) {
             $this->user->getPermissions()->validate($dbRole);
         }
     }
     if ($dbRole->origin == ROLE_TYPE::CUSTOM) {
         $variables = new Scalr_Scripting_GlobalVariables($this->getEnvironmentId(), Scalr_Scripting_GlobalVariables::SCOPE_ROLE);
         $variables->setValues(json_decode($this->getParam('variables'), true), $dbRole->id);
     }
     $dbRole->description = $this->getParam('description');
     foreach ($this->getParam('removedImages') as $imageId) {
         $dbRole->removeImage($imageId);
     }
     foreach ($this->getParam('images') as $image) {
         $image = (array) $image;
         $dbRole->setImage($image['image_id'], $image['platform'], $image['location'], $image['szr_version'], $image['architecture']);
     }
     $dbRole->setScripts($this->getParam('scripts'));
     //todo
     //$dbRole->setChefSettings($this->getParam('chef'));
     if ($this->user->isScalrAdmin()) {
         $dbRole->setTags($this->getParam('tags'));
     }
     $dbRole->save();
     $this->response->success('Role saved');
 }
All Usage Examples Of DBRole::save