Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
<?php
class Question {
private $id;
private $text;
private $created_at;
public function __construct(array $data = []) {
if (!empty($data)) {
$this->id = $data['id'] ?? null;
$this->text = $data['text'] ?? '';
$this->created_at = $data['created_at'] ?? null;
}
}
// Getters
public function getId(): ?int { return $this->id; }
public function getText(): string { return $this->text; }
public function getCreatedAt(): ?string { return $this->created_at; }
public function setText(string $text): void {
$this->text = $text;
}
// CRUD operations
public static function find(int $id): ?self {
$pdo = Database::getInstance()->getConnection();
$stmt = $pdo->prepare("SELECT * FROM questions WHERE id = :id");
$stmt->execute([':id' => $id]);
$data = $stmt->fetch();
return $data ? new self($data) : null;
}
public static function getAll(): array {
$pdo = Database::getInstance()->getConnection();
$stmt = $pdo->query("SELECT * FROM questions ORDER BY id DESC");
$questions = [];
while ($data = $stmt->fetch()) {
$questions[] = new self($data);
}
return $questions;
}
public function save(): bool {
$pdo = Database::getInstance()->getConnection();
if ($this->id) {
$stmt = $pdo->prepare("UPDATE questions SET text = :text WHERE id = :id");
return $stmt->execute([':text' => $this->text, ':id' => $this->id]);
} else {
$stmt = $pdo->prepare("INSERT INTO questions (text) VALUES (:text)");
$result = $stmt->execute([':text' => $this->text]);
if ($result) {
$this->id = $pdo->lastInsertId();
}
return $result;
}
}
public function delete(): bool {
if (!$this->id) return false;
$pdo = Database::getInstance()->getConnection();
$stmt = $pdo->prepare("DELETE FROM questions WHERE id = :id");
return $stmt->execute([':id' => $this->id]);
}
public static function create(string $text): bool {
$question = new self(['text' => $text]);
return $question->save();
}
}