File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 2929 <li >
3030 <a href =" {{ url(' page::category-resource' , {slug : category .slug }) }}" >
3131 {{ category .name }}
32- <span class =" cat-count" >{{ category .posts . count () }}</span >
32+ <span class =" cat-count" >{{ category .publishedPostsCount }}</span >
3333 </a >
3434 </li >
3535 {% endfor %}
Original file line number Diff line number Diff line change 66
77use Doctrine \Common \Collections \ArrayCollection ;
88use Doctrine \Common \Collections \Collection ;
9+ use Doctrine \Common \Collections \Criteria ;
10+ use Doctrine \Common \Collections \Selectable ;
911use Doctrine \ORM \Mapping as ORM ;
1012use Light \App \Entity \AbstractEntity ;
13+ use Light \Blog \Enum \PostStatusEnum ;
1114use Light \Blog \Repository \CategoryRepository ;
1215
1316#[ORM \Entity(repositoryClass: CategoryRepository::class)]
@@ -24,7 +27,7 @@ class Category extends AbstractEntity
2427 #[ORM \Column(name: 'isVisible ' , type: 'boolean ' )]
2528 private bool $ isVisible = true ;
2629
27- /** @var Collection<int, Post> */
30+ /** @var Collection<int, Post>&Selectable<int, Post> */
2831 #[ORM \OneToMany(mappedBy: 'category ' , targetEntity: Post::class)]
2932 private Collection $ posts ;
3033
@@ -73,6 +76,13 @@ public function getPosts(): Collection
7376 return $ this ->posts ;
7477 }
7578
79+ public function getPublishedPostsCount (): int
80+ {
81+ $ criteria = Criteria::create ()->where (Criteria::expr ()->eq ('status ' , PostStatusEnum::Published));
82+
83+ return $ this ->posts ->matching ($ criteria )->count ();
84+ }
85+
7686 /**
7787 * @return array{
7888 * id: non-empty-string,
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace LightTest \Unit \Blog \Entity ;
6+
7+ use Light \Blog \Entity \Category ;
8+ use Light \Blog \Entity \Post ;
9+ use Light \Blog \Enum \PostStatusEnum ;
10+ use LightTest \Unit \UnitTest ;
11+
12+ class CategoryTest extends UnitTest
13+ {
14+ public function testGetPublishedPostsCountOnlyCountsPublishedPosts (): void
15+ {
16+ $ category = new Category ();
17+
18+ $ published = new Post ();
19+ $ published ->setStatus (PostStatusEnum::Published);
20+
21+ $ draft = new Post ();
22+ $ draft ->setStatus (PostStatusEnum::Draft);
23+
24+ $ private = new Post ();
25+ $ private ->setStatus (PostStatusEnum::Private);
26+
27+ $ category ->getPosts ()->add ($ published );
28+ $ category ->getPosts ()->add ($ draft );
29+ $ category ->getPosts ()->add ($ private );
30+
31+ self ::assertSame (1 , $ category ->getPublishedPostsCount ());
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments