From b188dc8cd0addd0e4d866100724bad19d72fcc6d Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Tue, 6 Dec 2022 09:51:39 +0100 Subject: [PATCH] Affiche le nom d'utilisateur pour chaque commentaire --- src/Silex/Gateway/CommentGateway.php | 14 ++++++++++++-- src/Silex/Model/Comment.php | 11 +++++++++++ views/newsView.php | 2 +- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Silex/Gateway/CommentGateway.php b/src/Silex/Gateway/CommentGateway.php index 2a2a2de..b3ef534 100644 --- a/src/Silex/Gateway/CommentGateway.php +++ b/src/Silex/Gateway/CommentGateway.php @@ -43,7 +43,7 @@ class CommentGateway */ public function getByNewsId(int $id): array { - $req = $this->pdo->prepare('SELECT * FROM comment WHERE news_id = :id ORDER BY publication_date DESC'); + $req = $this->pdo->prepare('SELECT c.*, u.login author_login FROM comment c INNER JOIN registered_user u ON u.id_user = c.author_id WHERE c.news_id = :id ORDER BY c.publication_date ASC'); $req->bindValue(':id', $id, PDO::PARAM_INT); if (!$req->execute()) { return []; @@ -76,6 +76,16 @@ class CommentGateway private function createComment(array $data): Comment { - return new Comment(intval($data['id_comment']),intval($data['news_id']), DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date']), $data['content'], intval($data['author_id'])); + $comment = new Comment( + intval($data['id_comment']), + intval($data['news_id']), + DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date']), + $data['content'], + intval($data['author_id']) + ); + if (isset($data['author_login'])) { + $comment->setAuthorLogin($data['author_login']); + } + return $comment; } } \ No newline at end of file diff --git a/src/Silex/Model/Comment.php b/src/Silex/Model/Comment.php index f5ef1ff..fd57121 100644 --- a/src/Silex/Model/Comment.php +++ b/src/Silex/Model/Comment.php @@ -13,6 +13,7 @@ class Comment private DateTime $publicationDate; private string $content; private int $authorId; + private string $authorLogin; public function __construct(int $idComment, int $newsId, DateTime $publicationDate, string $content, int $authorId) { @@ -48,8 +49,18 @@ class Comment return $this->authorId; } + public function getAuthorLogin(): string + { + return $this->authorLogin; + } + public function setId(int $id): void { $this->idComment = $id; } + + public function setAuthorLogin(string $authorLogin): void + { + $this->authorLogin = $authorLogin; + } } \ No newline at end of file diff --git a/views/newsView.php b/views/newsView.php index c644527..cd52780 100644 --- a/views/newsView.php +++ b/views/newsView.php @@ -17,7 +17,7 @@
- From getAuthorId() ?> published on getPublicationDate()->format('Y-m-d H:i:s') ?> + From getAuthorLogin() ?> published on getPublicationDate()->format('Y-m-d H:i:s') ?>
getContent() ?>