public function save()
{
$this->changed = microtime();
$db = $this->db();
try {
$db->BeginTrans();
if (empty($this->id)) {
$this->ownerId = $this->changedById;
$this->settings[FarmSetting::CREATED_BY_ID] = $this->ownerId;
$this->settings[FarmSetting::CREATED_BY_EMAIL] = User::findPk($this->ownerId)->email;
$this->added = new DateTime();
$this->status = FARM_STATUS::TERMINATED;
if (empty($this->settings[FarmSetting::TIMEZONE])) {
$this->settings[FarmSetting::TIMEZONE] = date_default_timezone_get();
}
$this->settings[FarmSetting::CRYPTO_KEY] = \Scalr::GenerateRandomKey(40);
/* @var $governance Governance */
$governance = Governance::findPk($this->envId, Governance::CATEGORY_GENERAL, Governance::GENERAL_LEASE);
if (!empty($governance) && $governance->enabled) {
$this->settings[FarmSetting::LEASE_STATUS] = 'Active';
}
//TODO: unused field
$this->region = Aws::REGION_US_EAST_1;
}
parent::save();
if (!empty($this->_settings)) {
$this->_settings->save();
}
if (isset($this->_teamsChanged)) {
$this->saveTeams($this->_teams);
$this->_teamsChanged = false;
}
$db->CommitTrans();
} catch (Exception $e) {
$db->RollbackTrans();
throw $e;
}
}