From bc1a6b2294d2b130b362bac0dbd1f76d37fd05e8 Mon Sep 17 00:00:00 2001 From: OStefan2001 Date: Wed, 12 Aug 2026 15:35:21 +0300 Subject: [PATCH 1/2] Fixed categories counter by published --- src/App/templates/partial/left-menu.html.twig | 2 +- src/Blog/src/Entity/Category.php | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/App/templates/partial/left-menu.html.twig b/src/App/templates/partial/left-menu.html.twig index 740d9b2b..1fc59b96 100644 --- a/src/App/templates/partial/left-menu.html.twig +++ b/src/App/templates/partial/left-menu.html.twig @@ -29,7 +29,7 @@
  • {{ category.name }} - {{ category.posts.count() }} + {{ category.publishedPostsCount }}
  • {% endfor %} diff --git a/src/Blog/src/Entity/Category.php b/src/Blog/src/Entity/Category.php index ce812c30..62233a0a 100644 --- a/src/Blog/src/Entity/Category.php +++ b/src/Blog/src/Entity/Category.php @@ -6,8 +6,11 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; +use Doctrine\Common\Collections\Criteria; +use Doctrine\Common\Collections\Selectable; use Doctrine\ORM\Mapping as ORM; use Light\App\Entity\AbstractEntity; +use Light\Blog\Enum\PostStatusEnum; use Light\Blog\Repository\CategoryRepository; #[ORM\Entity(repositoryClass: CategoryRepository::class)] @@ -24,7 +27,7 @@ class Category extends AbstractEntity #[ORM\Column(name: 'isVisible', type: 'boolean')] private bool $isVisible = true; - /** @var Collection */ + /** @var Collection&Selectable */ #[ORM\OneToMany(mappedBy: 'category', targetEntity: Post::class)] private Collection $posts; @@ -73,6 +76,13 @@ public function getPosts(): Collection return $this->posts; } + public function getPublishedPostsCount(): int + { + $criteria = Criteria::create()->where(Criteria::expr()->eq('status', PostStatusEnum::Published)); + + return $this->posts->matching($criteria)->count(); + } + /** * @return array{ * id: non-empty-string, From 5fad1673df03c5ec2d7371efb0bd524a8f2c53a4 Mon Sep 17 00:00:00 2001 From: OStefan2001 Date: Wed, 12 Aug 2026 15:40:02 +0300 Subject: [PATCH 2/2] Add test --- test/Unit/Blog/Entity/CategoryTest.php | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 test/Unit/Blog/Entity/CategoryTest.php diff --git a/test/Unit/Blog/Entity/CategoryTest.php b/test/Unit/Blog/Entity/CategoryTest.php new file mode 100644 index 00000000..bea5035a --- /dev/null +++ b/test/Unit/Blog/Entity/CategoryTest.php @@ -0,0 +1,33 @@ +setStatus(PostStatusEnum::Published); + + $draft = new Post(); + $draft->setStatus(PostStatusEnum::Draft); + + $private = new Post(); + $private->setStatus(PostStatusEnum::Private); + + $category->getPosts()->add($published); + $category->getPosts()->add($draft); + $category->getPosts()->add($private); + + self::assertSame(1, $category->getPublishedPostsCount()); + } +}