Skip to content

Commit 5bf8736

Browse files
committed
* replace key forum with forums that contains all forum of queried posts
* fix prop `$latestReplierId` of `App\Entity\Post\Thread` might be `null` in `$latestRepliersIdKeyByFid` @ `App\Controller\PostsController::query()` * rename method `getForum()` to `getForums()` as now querying against multiple forums @ `ForumRepository` * now return a merge array instead of `Collection` @ `LatestReplierRepository::getLatestRepliersWithoutNameWhenHasUid()` @ `App\Repository` @ be
1 parent 1e03857 commit 5bf8736

3 files changed

Lines changed: 16 additions & 13 deletions

File tree

be/src/Controller/PostsController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function query(Request $request): array
6565
$this->stopwatch->start('queryUsers');
6666
$latestRepliersIdKeyByFid = $this->query->postsTree->threads
6767
->map(fn(Thread $thread) => ['fid' => $thread->getFid(), 'latestReplierId' => $thread->getLatestReplierId()])
68+
->filter(fn(array $fidAndLatestReplierId) => $fidAndLatestReplierId['latestReplierId'] !== null)
6869
->groupBy(fn(array $fidAndLatestReplierId) => $fidAndLatestReplierId['fid'])
6970
->map(fn(Collection $fidAndLatestRepliersUid) => $fidAndLatestRepliersUid->pluck('latestReplierId'));
7071
$latestRepliers = $this->latestReplierRepository->getLatestRepliersWithoutNameWhenHasUid($latestRepliersIdKeyByFid->flatten());
@@ -73,7 +74,7 @@ public function query(Request $request): array
7374
$this->query->postsTree->replies,
7475
$this->query->postsTree->subReplies
7576
])->flatten();
76-
$latestRepliersUidKeyById = $latestRepliers
77+
$latestRepliersUidKeyById = collect($latestRepliers)
7778
->mapWithKeys(fn(array|LatestReplier $latestReplier) => [
7879
is_array($latestReplier) ? $latestReplier['id'] : $latestReplier->getId() =>
7980
is_array($latestReplier) ? $latestReplier['uid'] : $latestReplier->getUid()
@@ -112,6 +113,7 @@ public function query(Request $request): array
112113
->map(fn(int $uid) => $users->get($uid)?->getPortrait())
113114
->filter(fn(?string $portrait) => $portrait !== null))
114115
))->keyBy(fn(ForumModerator $forumModerator) => $forumModerator->portrait);
116+
115117
$users = $users->each(fn(User $user) => $user->setForumSpecific([
116118
'authorExpGrades' => $authorExpGrades->get($user->getUid()),
117119
'forumModerators' => $forumModerators->get($user->getPortrait())
@@ -124,7 +126,9 @@ public function query(Request $request): array
124126
'nextCursor' => $this->query->queryResult->nextCursor,
125127
...$matchQueryPostCounts,
126128
],
127-
'forum' => $this->forumRepository->getForum($fid),
129+
'forums' => collect($this->forumRepository
130+
->getForums($posts->map(fn(Post $post) => $post->getFid())->unique())
131+
)->mapWithKeys(fn(array $forum) => [$forum['fid'] => $forum['name']]),
128132
'threads' => $this->query->postsTree->reOrderNestedPosts(
129133
$this->query->postsTree->nestPostsWithParent(),
130134
$this->query->getOrderByField(),

be/src/Repository/ForumRepository.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ public function isForumExists(int $fid): bool
2525
return $this->isEntityExists($dql, ['fid' => $fid]);
2626
}
2727

28-
public function getForum(int $fid): array
28+
public function getForums(array|\ArrayAccess $fids): array
2929
{
30-
$dql = 'SELECT t.fid, t.name FROM App\Entity\Forum t WHERE t.fid = :fid';
31-
return $this->createQueryWithParams($dql, ['fid' => $fid])
32-
->setMaxResults(1)->getSingleResult();
30+
$dql = 'SELECT t.fid, t.name FROM App\Entity\Forum t WHERE t.fid IN (:fids)';
31+
return $this->createQueryWithParams($dql, ['fids' => $fids])->getResult();
3332
}
3433
}

be/src/Repository/LatestReplierRepository.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ public function __construct(ManagerRegistry $registry)
1414
parent::__construct($registry, LatestReplier::class);
1515
}
1616

17-
public function getLatestRepliersWithoutNameWhenHasUid(array|\ArrayAccess $latestRepliersId): Collection
17+
/** @return list<array{id: int, uid: int, createdAt: int, updatedAt: int}|LatestReplier> */
18+
public function getLatestRepliersWithoutNameWhenHasUid(array|\ArrayAccess $latestRepliersId): array
1819
{
19-
// removeSelect('t.name', 't.displayName')
20-
return collect($this->getQueryResultWithParams(<<<'DQL'
20+
return [ // removeSelect('t.name', 't.displayName')
21+
...$this->getQueryResultWithParams(<<<'DQL'
2122
SELECT t.id, t.uid, t.createdAt, t.updatedAt
2223
FROM App\Entity\LatestReplier t
2324
WHERE t.id IN (:ids) AND t.uid IS NOT NULL
24-
DQL, ['ids' => $latestRepliersId]))
25-
->concat(
26-
$this->getQueryResultWithParams(<<<'DQL'
25+
DQL, ['ids' => $latestRepliersId]),
26+
...$this->getQueryResultWithParams(<<<'DQL'
2727
SELECT t FROM App\Entity\LatestReplier t
2828
WHERE t.id IN (:ids) AND t.uid IS NULL
2929
DQL, ['ids' => $latestRepliersId]),
30-
);
30+
];
3131
}
3232
}

0 commit comments

Comments
 (0)