고급 모듈 예
고급 모듈 예제 - 포럼
섹션 제목: “고급 모듈 예제 - 포럼”다양한 엔터티 유형, 관계, 복잡한 관리 인터페이스 및 알림 등 고급 패턴을 보여주는 포괄적인 “포럼” 모듈입니다.
모듈 구조
섹션 제목: “모듈 구조”forum/├── xoops_version.php├── class/│ ├── Repository/│ │ ├── ForumRepository.php│ │ ├── TopicRepository.php│ │ └── PostRepository.php│ ├── Entity/│ │ ├── Forum.php│ │ ├── Topic.php│ │ └── Post.php│ ├── Service/│ │ ├── ForumService.php│ │ └── TopicService.php│ └── Handler/│ └── NotificationHandler.php├── templates/│ ├── forum_list.html│ ├── topic_view.html│ └── admin/dashboard.html└── sql/mysql.sql데이터베이스 스키마
섹션 제목: “데이터베이스 스키마”-- ForumsCREATE TABLE `xoops_forum_forums` ( `forum_id` INT AUTO_INCREMENT PRIMARY KEY, `forum_name` VARCHAR(255) NOT NULL, `forum_description` TEXT, `forum_order` INT, `forum_created` INT NOT NULL);
-- TopicsCREATE TABLE `xoops_forum_topics` ( `topic_id` INT AUTO_INCREMENT PRIMARY KEY, `topic_forum_id` INT NOT NULL, `topic_author_id` INT NOT NULL, `topic_title` VARCHAR(255) NOT NULL, `topic_post_count` INT DEFAULT 1, `topic_view_count` INT DEFAULT 0, `topic_created` INT NOT NULL, FOREIGN KEY (`topic_forum_id`) REFERENCES `xoops_forum_forums`(`forum_id`));
-- PostsCREATE TABLE `xoops_forum_posts` ( `post_id` INT AUTO_INCREMENT PRIMARY KEY, `post_topic_id` INT NOT NULL, `post_forum_id` INT NOT NULL, `post_author_id` INT NOT NULL, `post_content` LONGTEXT NOT NULL, `post_created` INT NOT NULL, FOREIGN KEY (`post_topic_id`) REFERENCES `xoops_forum_topics`(`topic_id`));
-- SubscriptionsCREATE TABLE `xoops_forum_subscriptions` ( `subscription_id` INT AUTO_INCREMENT PRIMARY KEY, `subscription_user_id` INT NOT NULL, `subscription_topic_id` INT NOT NULL, UNIQUE (`subscription_user_id`, `subscription_topic_id`));엔터티 클래스
섹션 제목: “엔터티 클래스”주제 엔터티
섹션 제목: “주제 엔터티”<?phpclass Topic{ private $id; private $forumId; private $authorId; private $title; private $postCount = 1; private $viewCount = 0; private $createdAt;
// Getters and setters... public function getId() { return $this->id; } public function setId($id) { $this->id = $id; return $this; }
public function getForumId() { return $this->forumId; } public function setForumId($id) { $this->forumId = $id; return $this; }
public function getTitle() { return $this->title; } public function setTitle($t) { $this->title = $t; return $this; }
public function getPostCount() { return $this->postCount; } public function incrementPostCount() { $this->postCount++; return $this; }
public function getViewCount() { return $this->viewCount; } public function incrementViewCount() { $this->viewCount++; return $this; }}?>관계가 있는 저장소
섹션 제목: “관계가 있는 저장소”<?phpclass TopicRepository{ private $db;
public function __construct($connection) { $this->db = $connection; }
public function getWithAuthorInfo($id) { $sql = "SELECT t.*, u.uname as author_name FROM " . $this->db->prefix('forum_topics') . " t LEFT JOIN " . $this->db->prefix('users') . " u ON t.topic_author_id = u.uid WHERE t.topic_id = ?";
$stmt = $this->db->prepare($sql); $stmt->bind_param('i', $id); $stmt->execute();
return $stmt->get_result()->fetch_assoc(); }
public function getByForumWithStats($forumId, $limit = 20, $offset = 0) { $sql = "SELECT t.*, u.uname as author_name, COUNT(p.post_id) as post_count FROM " . $this->db->prefix('forum_topics') . " t LEFT JOIN " . $this->db->prefix('users') . " u ON t.topic_author_id = u.uid LEFT JOIN " . $this->db->prefix('forum_posts') . " p ON t.topic_id = p.post_topic_id WHERE t.topic_forum_id = ? GROUP BY t.topic_id ORDER BY t.topic_created DESC LIMIT ?, ?";
$stmt = $this->db->prepare($sql); $stmt->bind_param('iii', $forumId, $offset, $limit); $stmt->execute();
$result = $stmt->get_result(); $topics = []; while ($row = $result->fetch_assoc()) { $topics[] = $row; }
return $topics; }}?>서비스 계층
섹션 제목: “서비스 계층”<?phpclass TopicService{ private $topicRepository; private $postRepository; private $notificationHandler;
public function __construct( TopicRepository $topicRepository, PostRepository $postRepository, NotificationHandler $notificationHandler ) { $this->topicRepository = $topicRepository; $this->postRepository = $postRepository; $this->notificationHandler = $notificationHandler; }
public function createTopic($forumId, $userId, $title, $content) { // Validate if (strlen($title) < 3) { throw new \InvalidArgumentException('Title too short'); }
// Create topic $topic = new Topic(); $topic->setForumId($forumId) ->setAuthorId($userId) ->setTitle($title) ->setCreatedAt(new \DateTime());
$topicId = $this->topicRepository->save($topic);
// Create first post $this->postRepository->createPost($topicId, $forumId, $userId, $content);
// Notify subscribers $this->notificationHandler->notifyNewTopic($topicId);
return $topicId; }
public function getTopicWithPosts($topicId, $page = 1, $perPage = 20) { // Get topic with author info $topic = $this->topicRepository->getWithAuthorInfo($topicId);
if (!$topic) { throw new \RuntimeException('Topic not found'); }
// Increment view count $topic['topic_view_count']++; $this->topicRepository->updateViewCount($topicId, $topic['topic_view_count']);
// Get posts $offset = ($page - 1) * $perPage; $posts = $this->postRepository->getByTopicId($topicId, $perPage, $offset);
return [ 'topic' => $topic, 'posts' => $posts, 'page' => $page, 'totalPages' => ceil($topic['topic_post_count'] / $perPage), ]; }}?>고급 기능
섹션 제목: “고급 기능”이 예에서는 다음을 보여줍니다.
- 엔티티 관계 - 포럼에는 주제가 포함되고, 주제에는 게시물이 포함됩니다.
- 복잡한 쿼리 - 사용자 정보 및 통계와 조인
- 서비스 조정 - 여러 서비스가 함께 작동
- 데이터 집계 - 게시물 수, 조회수
- 알림 - 구독에 대한 이벤트 기반 알림
- 트랜잭션과 유사한 작업 - 초기 게시물로 주제 만들기
관련 패턴
섹션 제목: “관련 패턴”참조:
- 복잡한 쿼리를 위한../Patterns/Repository-Pattern
- 서비스 조정을 위한../Patterns/Service-Layer
- 데이터 전송을 위한../Patterns/DTO-Pattern
태그: #예제 #고급 모듈 #복잡한 예 #관계 #모듈 개발