aboutsummaryrefslogtreecommitdiffhomepage
path: root/Digitigrade/Model.php
diff options
context:
space:
mode:
Diffstat (limited to 'Digitigrade/Model.php')
-rw-r--r--Digitigrade/Model.php21
1 files changed, 19 insertions, 2 deletions
diff --git a/Digitigrade/Model.php b/Digitigrade/Model.php
index 80a6d91..0db27ef 100644
--- a/Digitigrade/Model.php
+++ b/Digitigrade/Model.php
@@ -10,8 +10,8 @@ abstract class Model {
}
/**
- * @return ?string a condition for a WHERE clause if this object already
- * exists in the database, or null otherwise
+ * @return ?string a condition for a WHERE clause that uniquely identifies
+ * this object if it already exists in the database, or null if not
*/
protected function getUpdateWhereClause(\PDO $db): ?string {
if (isset($this->id) && static::find($this->{'id'}) != null) {
@@ -241,4 +241,21 @@ abstract class Model {
$this->saved = true;
}
+
+ /**
+ * Removes this record from the database.
+ * @return void
+ */
+ public function remove() {
+ $db = Db::getInstance()->getPdo();
+ $where = $this->getUpdateWhereClause($db);
+ if ($where == null) {
+ throw new \RuntimeException("object doesn't seem to exist in the database so i can't delete it");
+ }
+ $classNameParts = explode('\\', static::class);
+ $className = $classNameParts[count($classNameParts) - 1];
+ $tableName = self::mangleName($className);
+
+ $db->exec("DELETE FROM $tableName WHERE $where");
+ }
} \ No newline at end of file