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
+17 -9
View File
@@ -25,23 +25,26 @@ class DatabaseArticleManager implements ArticleManagerDAO {
// Tabelle für Beiträge // Tabelle für Beiträge
$db->exec(" $db->exec("
CREATE TABLE articles ( CREATE TABLE IF NOT EXISTS articles (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT, title TEXT,
content TEXT, content TEXT,
author TEXT, author TEXT,
category TEXT, category TEXT,
tags TEXT, tags TEXT,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (author) REFERENCES users(email) ON DELETE SET NULL ON UPDATE CASCADE
);"); );");
// Tabelle für Likes // Tabelle für Likes
$db->exec(" $db->exec("
CREATE TABLE likes ( CREATE TABLE IF NOT EXISTS likes (
article_id INTEGER, article_id INTEGER,
user_id TEXT, user_id TEXT,
PRIMARY KEY (article_id, user_id), 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); unset($db);
} catch (PDOException $e) { } catch (PDOException $e) {
@@ -52,17 +55,22 @@ class DatabaseArticleManager implements ArticleManagerDAO {
/** /**
* Baut die Verbindung zur Datenbank auf. * Baut die Verbindung zur Datenbank auf.
* @throws InternalServerErrorException *
* @return PDO Datenbankverbindung
* @throws RuntimeException
*/ */
private function getConnection() private function getConnection()
{ {
try { try {
$user = 'root';
$pw = null;
$dsn = 'sqlite:' . __DIR__ . '/../../db/eduforgeDB.db'; $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) { } catch (PDOException $e) {
throw new InternalServerErrorException($e->getMessage()); throw new RuntimeException("internal_error");
} }
} }
+5 -1
View File
@@ -28,7 +28,10 @@ class DatabaseCommentManager implements CommentManagerDAO
parent_comment_id INTEGER NULL, parent_comment_id INTEGER NULL,
author TEXT NOT NULL, author TEXT NOT NULL,
content TEXT NOT NULL, content TEXT NOT NULL,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP 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
); );
"); ");
@@ -63,6 +66,7 @@ class DatabaseCommentManager implements CommentManagerDAO
$db = new PDO($dsn, null, null); $db = new PDO($dsn, null, null);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec('PRAGMA foreign_keys = ON;');
return $db; return $db;
+1
View File
@@ -55,6 +55,7 @@ class DatabaseUserManager implements UserManagerDAO {
$db = new PDO($dsn, null, null); $db = new PDO($dsn, null, null);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec('PRAGMA foreign_keys = ON;');
return $db; return $db;