/** * {@inheritdoc} * @see AbstractEntity::delete() * * @param bool $force Delete ignoring restrictions */ public function delete($force = false) { $db = $this->db(); if (!$force) { if ($db->GetOne("SELECT 1 FROM `farms` WHERE `env_id` = ? LIMIT 1", [$this->id])) { throw new ObjectInUseException('Cannot remove environment. You need to remove all your farms first.'); } if ($db->GetOne("SELECT COUNT(*) FROM client_environments WHERE client_id = ?", [$this->accountId]) < 2) { throw new ObjectInUseException('At least one environment should be in account. You cannot remove the last one.'); } } parent::delete(); try { $db->Execute("DELETE FROM client_environment_properties WHERE env_id=?", [$this->id]); $db->Execute("DELETE FROM apache_vhosts WHERE env_id=?", [$this->id]); $db->Execute("DELETE FROM autosnap_settings WHERE env_id=?", [$this->id]); $db->Execute("DELETE FROM bundle_tasks WHERE env_id=?", [$this->id]); $db->Execute("DELETE FROM dns_zones WHERE env_id=?", [$this->id]); $db->Execute("DELETE FROM ec2_ebs WHERE env_id=?", [$this->id]); $db->Execute("DELETE FROM elastic_ips WHERE env_id=?", [$this->id]); $db->Execute("DELETE FROM farms WHERE env_id=?", [$this->id]); $db->Execute("DELETE FROM roles WHERE env_id=?", [$this->id]); $servers = \DBServer::listByFilter(['envId' => $this->id]); foreach ($servers as $server) { /* @var $server \DBServer */ $server->Remove(); } Entity\EnvironmentCloudCredentials::deleteByEnvId($this->id); Entity\CloudCredentials::deleteByEnvId($this->id); TeamEnvs::deleteByEnvId($this->id); } catch (Exception $e) { throw new Exception(sprintf(_("Cannot delete record. Error: %s"), $e->getMessage()), $e->getCode()); } }