From a705c823e715e18fd0e2a2f4d4695975920c4d81 Mon Sep 17 00:00:00 2001 From: seto Date: Wed, 10 Jun 2026 16:16:10 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=E3=82=AB=E3=83=86=E3=82=B4=E3=83=AA?= =?UTF-8?q?=E3=81=94=E3=81=A8=E3=81=AE=E8=A8=98=E4=BA=8B=E6=95=B0=E3=81=AE?= =?UTF-8?q?=E9=9B=86=E8=A8=88=E3=82=92=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Model/Table/BlogCategoriesTable.php | 82 +++++++++++++++---- 1 file changed, 66 insertions(+), 16 deletions(-) diff --git a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php index 12df3d7286..5ddb6826ac 100755 --- a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php +++ b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php @@ -224,8 +224,12 @@ protected function _getCategoryList( 'siteId' => null, 'order' => 'BlogCategories.lft asc', 'conditions' => [], - 'threaded' => false + 'threaded' => false, + 'categoryPostCounts' => [], ], $options); + if ($viewCount && empty($options['categoryPostCounts'])) { + $options['categoryPostCounts'] = $this->getCategoryPostCounts($blogContentId); + } // 検索条件 $conditions = $options['conditions']; @@ -261,7 +265,6 @@ protected function _getCategoryList( // 検索実行 $query = $this->find($findType) - ->contain(['BlogPosts' => ['BlogContents' => ['Contents']]]) ->where($conditions) ->select($fields) ->orderBy($options['order']); @@ -280,20 +283,7 @@ protected function _getCategoryList( foreach ($entities as $entity) { // 表示件数 if ($viewCount) { - $childrenIds = $this->find('list', keyField: 'id', valueField: 'id') - ->where([ - ['BlogCategories.lft > ' => $entity->lft], - ['BlogCategories.rght < ' => $entity->rght] - ])->toArray(); - $categoryId = [$entity->id]; - if ($childrenIds) { - $categoryId = array_merge($categoryId, $childrenIds); - } - $entity->count = $this->BlogPosts->find() - ->where(array_merge( - ['BlogPosts.blog_category_id IN' => $categoryId], - $this->BlogPosts->getConditionAllowPublish() - ))->count(); + $entity->count = $options['categoryPostCounts'][$entity->id] ?? 0; } // 子カテゴリ if ($current < $depth) { @@ -313,6 +303,66 @@ protected function _getCategoryList( return $entities; } + /** + * カテゴリごとの記事数を集計 + * + * @param int|null $blogContentId + * @return array + */ + private function getCategoryPostCounts(?int $blogContentId): array + { + $conditions = []; + if ($blogContentId) { + $conditions['BlogCategories.blog_content_id'] = $blogContentId; + } + $categories = $this->find() + ->select(['id', 'lft', 'rght']) + ->where($conditions) + ->all() + ->toList(); + if (!$categories) { + return []; + } + + $postCounts = []; + $postConditions = [ + 'blog_category_id IN' => array_column($categories, 'id'), + ...$this->BlogPosts->getConditionAllowPublish() + ]; + $query = $this->BlogPosts->find() + ->select([ + 'blog_category_id', + 'post_count' => $this->BlogPosts->find()->func()->count('*') + ]) + ->where($postConditions) + ->groupBy('blog_category_id'); + foreach ($query as $countRow) { + $postCounts[$countRow['blog_category_id']] = $countRow['post_count']; + } + + $categoryPostCounts = []; + foreach ($categories as $category) { + $categoryId = $category['id']; + $totalCount = $postCounts[$categoryId] ?? 0; + + // 子カテゴリの記事件数を親へ加算 + foreach ($categories as $targetCategory) { + $targetCategoryId = $targetCategory['id']; + if ($targetCategoryId === $categoryId) { + continue; + } + if ($targetCategory['lft'] <= $category['lft'] || $targetCategory['rght'] >= $category['rght']) { + continue; + } + $totalCount += $postCounts[$targetCategoryId] ?? 0; + } + + $categoryPostCounts[$categoryId] = $totalCount; + } + + return $categoryPostCounts; + } + /** * アクセス制限としてカテゴリの新規追加ができるか確認する * From dd580a2ab60a1df6f8655cf4dc23cab27d55d39a Mon Sep 17 00:00:00 2001 From: seto1 <30764014+seto1@users.noreply.github.com> Date: Thu, 11 Jun 2026 11:59:36 +0900 Subject: [PATCH 2/3] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php index 5ddb6826ac..8718b118a9 100755 --- a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php +++ b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php @@ -226,8 +226,7 @@ protected function _getCategoryList( 'conditions' => [], 'threaded' => false, 'categoryPostCounts' => [], - ], $options); - if ($viewCount && empty($options['categoryPostCounts'])) { + if ($viewCount && !$options['threaded'] && empty($options['categoryPostCounts'])) { $options['categoryPostCounts'] = $this->getCategoryPostCounts($blogContentId); } From 23974278c9d05bc533c51ee5bfbf491f50f18b3a Mon Sep 17 00:00:00 2001 From: seto Date: Thu, 11 Jun 2026 12:07:41 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=E6=8C=87=E6=91=98=E4=BA=8B=E9=A0=85?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php index 8718b118a9..0e33f367fd 100755 --- a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php +++ b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php @@ -328,10 +328,10 @@ private function getCategoryPostCounts(?int $blogContentId): array 'blog_category_id IN' => array_column($categories, 'id'), ...$this->BlogPosts->getConditionAllowPublish() ]; - $query = $this->BlogPosts->find() - ->select([ + $query = $this->BlogPosts->find(); + $query->select([ 'blog_category_id', - 'post_count' => $this->BlogPosts->find()->func()->count('*') + 'post_count' => $query->func()->count('*'), ]) ->where($postConditions) ->groupBy('blog_category_id');