blob: 464cfe2881c3d91be3ef0fd6a259198a0ca9c895 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<?php
declare(strict_types=1);
// Comment model: database operations for the comments table.
namespace App\Models;
use App\Database;
class Comment
{
private \PDO $pdo;
public function __construct()
{
$this->pdo = Database::getInstance()->getPdo();
}
public function create(int $userId, int $postId, string $content): int
{
$stmt = $this->pdo->prepare(
'INSERT INTO comments (user_id, post_id, content) VALUES (:user_id, :post_id, :content)'
);
$stmt->execute(['user_id' => $userId, 'post_id' => $postId, 'content' => $content]);
return (int) $this->pdo->lastInsertId();
}
public function findByPostId(int $postId): array
{
$stmt = $this->pdo->prepare(
'SELECT comments.*, users.username FROM comments
JOIN users ON comments.user_id = users.id
WHERE comments.post_id = :post_id
ORDER BY comments.created_at ASC'
);
$stmt->execute(['post_id' => $postId]);
return $stmt->fetchAll();
}
}
|