X-Git-Url: https://git.cweiske.de/anoweco.git/blobdiff_plain/15d4e6d1249487c633b42898407b567b9061b116..506949fa5639b8810a2437a9bffbe40165e4d8ae:/src/anoweco/Storage.php diff --git a/src/anoweco/Storage.php b/src/anoweco/Storage.php index 4508f10..6a7e204 100644 --- a/src/anoweco/Storage.php +++ b/src/anoweco/Storage.php @@ -92,6 +92,49 @@ class Storage return $json; } + /** + * @return null|object NULL if not found, JSON comment object otherwise + * - "Xrow" property contains the database row object + * - "user" property contains the user db row object + */ + public function listLatest() + { + $stmt = $this->db->prepare( + 'SELECT comment_id, comment_user_id, comment_published' + . ', comment_of_url, comment_type' + . ' FROM comments' + . ' ORDER BY comment_published DESC' + . ' LIMIT 20' + ); + $stmt->execute(); + $rows = $stmt->fetchAll(\PDO::FETCH_OBJ); + if (!count($rows)) { + return []; + } + $userIds = array_values( + array_unique(array_column($rows, 'comment_user_id')) + ); + + + $placeholders = implode(',', array_fill(0, count($userIds), '?')); + $stmt = $this->db->prepare( + 'SELECT * FROM users WHERE user_id IN (' . $placeholders . ')' + ); + $stmt->execute($userIds); + + $users = $stmt->fetchAll(\PDO::FETCH_OBJ); + $users = array_combine( + array_column($users, 'user_id'), + $users + ); + + foreach ($rows as $row) { + $row->user = $users[$row->comment_user_id]; + } + + return $rows; + } + /** * @return null|object NULL if not found, user database row otherwise */