/** * Removes an environment from the database. * * @param \commpress\Cli\Entity\Environment $environment * The environment to remove. * * @throws \commpress\Cli\Service\Database\Exception\DatabaseQueryException * If something goes wrong. */ public function removeEnvironment(Environment $environment) { $query = sprintf('DELETE FROM %s WHERE %s = :id', self::TABLE_ENVIRONMENTS, Environment::FIELD_ID); $stmt = $this->database->prepare($query); $stmt->bindValue(':' . Environment::FIELD_ID, $environment->getId()); $result = $stmt->execute(); $stmt->close(); if (!$result) { throw new DatabaseQueryException(sprintf('Failed to remove environment [ id: %d ] from database.', $environment->getId())); } }
/** * Checks if the entity by id exists already in the database. * * @return bool * TRUE if the entity is stored in the database, otherwise false. */ public function entityExists() { // Before we ask the database we want to make sure we have an id. if ($this->environment->getId() == null) { // No database request is needed in this case. return false; } // Define the query. $query = sprintf('SELECT COUNT(id) AS num FROM environments WHERE %s = :id', Environment::FIELD_ID); // Get the database instance. $database = Application::getDatabase(); // Prepare the statement. $stmt = $database->prepare($query); $stmt->bindValue(':id', $this->environment->getId()); // Get the result from statement execution. $result = $stmt->execute()->fetchArray(SQLITE3_ASSOC); // Close the statement to free memory. $stmt->close(); // Check if the project was found. if ($result['num'] >= 1) { return true; } return false; }