Fremdschlüssel hinzugefügt

This commit is contained in:
NOrtmann1
2026-06-24 15:59:41 +02:00
parent 5030caa029
commit ed31210e3a
3 changed files with 36 additions and 23 deletions
+24 -16
View File
@@ -25,23 +25,26 @@ class DatabaseArticleManager implements ArticleManagerDAO {
// Tabelle für Beiträge
$db->exec("
CREATE TABLE articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
content TEXT,
author TEXT,
category TEXT,
tags TEXT,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);");
CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
content TEXT,
author TEXT,
category TEXT,
tags TEXT,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author) REFERENCES users(email) ON DELETE SET NULL ON UPDATE CASCADE
);");
// Tabelle für Likes
$db->exec("
CREATE TABLE likes (
CREATE TABLE IF NOT EXISTS likes (
article_id INTEGER,
user_id TEXT,
PRIMARY KEY (article_id, user_id),
FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE
FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(email) ON DELETE CASCADE
);");
unset($db);
} catch (PDOException $e) {
@@ -52,17 +55,22 @@ class DatabaseArticleManager implements ArticleManagerDAO {
/**
* Baut die Verbindung zur Datenbank auf.
* @throws InternalServerErrorException
*
* @return PDO Datenbankverbindung
* @throws RuntimeException
*/
private function getConnection()
{
try {
$user = 'root';
$pw = null;
$dsn = 'sqlite:' . __DIR__ . '/../../db/eduforgeDB.db';
return new PDO($dsn, $user, $pw);
$db = new PDO($dsn, null, null);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec('PRAGMA foreign_keys = ON;');
return $db;
} catch (PDOException $e) {
throw new InternalServerErrorException($e->getMessage());
throw new RuntimeException("internal_error");
}
}
+11 -7
View File
@@ -23,13 +23,16 @@ class DatabaseCommentManager implements CommentManagerDAO
$db->exec("
CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
article_id INTEGER NOT NULL,
parent_comment_id INTEGER NULL,
author TEXT NOT NULL,
content TEXT NOT NULL,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
id INTEGER PRIMARY KEY AUTOINCREMENT,
article_id INTEGER NOT NULL,
parent_comment_id INTEGER NULL,
author TEXT NOT NULL,
content TEXT NOT NULL,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (article_id) REFERENCES articles(id) ON DELETE CASCADE,
FOREIGN KEY (author) REFERENCES users(email) ON DELETE CASCADE,
FOREIGN KEY (parent_comment_id) REFERENCES comments(id) ON DELETE CASCADE
);
");
$columns = $db->query("PRAGMA table_info(comments);")->fetchAll(PDO::FETCH_ASSOC);
@@ -63,6 +66,7 @@ class DatabaseCommentManager implements CommentManagerDAO
$db = new PDO($dsn, null, null);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec('PRAGMA foreign_keys = ON;');
return $db;
+1
View File
@@ -55,6 +55,7 @@ class DatabaseUserManager implements UserManagerDAO {
$db = new PDO($dsn, null, null);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec('PRAGMA foreign_keys = ON;');
return $db;