-
Notifications
You must be signed in to change notification settings - Fork 29
Dev #384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Dev #384
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ | |
|
|
||
| namespace PhpList\Core\Domain\Subscription\Repository; | ||
|
|
||
| use PhpList\Core\Domain\Common\Model\Filter\FilterRequestInterface; | ||
| use PhpList\Core\Domain\Common\Model\PaginatedResult; | ||
| use PhpList\Core\Domain\Common\Repository\AbstractRepository; | ||
| use PhpList\Core\Domain\Common\Repository\CursorPaginationTrait; | ||
| use PhpList\Core\Domain\Common\Repository\Interfaces\PaginatableRepositoryInterface; | ||
|
|
@@ -14,17 +16,92 @@ class SubscriberPageRepository extends AbstractRepository implements Paginatable | |
| { | ||
| use CursorPaginationTrait; | ||
|
|
||
| /** @return array{page: SubscribePage, data: SubscribePageData}[] */ | ||
| public function findPagesWithData(int $pageId): array | ||
| public function findPageWithData(int $pageId): ?SubscribePage | ||
| { | ||
| return $this->createQueryBuilder('p') | ||
| ->select('p AS page, d AS data') | ||
| ->from(SubscribePage::class, 'p') | ||
| ->from(SubscribePageData::class, 'd') | ||
| $result = $this->createQueryBuilder('p') | ||
| ->select('p AS page', 'd AS data') | ||
| ->leftJoin( | ||
| SubscribePageData::class, | ||
| 'd', | ||
| 'ON', | ||
| 'd.id = p.id' | ||
| ) | ||
| ->where('p.id = :id') | ||
| ->andWhere('d.id = p.id') | ||
| ->setParameter('id', $pageId) | ||
| ->getQuery() | ||
| ->getResult(); | ||
|
|
||
| if ($result === []) { | ||
| return null; | ||
| } | ||
|
|
||
| return $this->loadPageData($result); | ||
| } | ||
|
|
||
| public function getFilteredAfterId(FilterRequestInterface $filter): PaginatedResult | ||
| { | ||
| $queryBuilder = $this->createQueryBuilder('p'); | ||
|
|
||
| $countQb = clone $queryBuilder; | ||
| $total = (int) $countQb | ||
| ->select('COUNT(DISTINCT p.id)') | ||
| ->getQuery() | ||
| ->getSingleScalarResult(); | ||
|
|
||
| $rows = $queryBuilder | ||
| ->select('p AS page', 'd AS data') | ||
| ->leftJoin( | ||
| SubscribePageData::class, | ||
| 'd', | ||
| 'ON', | ||
| 'd.id = p.id' | ||
| ) | ||
| ->andWhere('p.id > :afterId') | ||
| ->setParameter('afterId', $filter->getLastId()) | ||
| ->orderBy('p.id', 'ASC') | ||
| ->setMaxResults($filter->getLimit()) | ||
| ->getQuery() | ||
| ->getResult(); | ||
|
|
||
| $grouped = []; | ||
| foreach ($rows as $row) { | ||
| /** @var SubscribePage $page */ | ||
| $page = $row['page'] ?? null; | ||
| $data = $row['data'] ?? null; | ||
| if ($page !== null) { | ||
| $grouped[$page->getId()][] = $row; | ||
| } | ||
| if ($data !== null) { | ||
| $grouped[$data->getId()][] = ['data' => $data]; | ||
| } | ||
| } | ||
|
|
||
| $pages = []; | ||
| foreach ($grouped as $group) { | ||
| $pages[] = $this->loadPageData($group); | ||
| } | ||
|
|
||
| return new PaginatedResult( | ||
| items: array_values($pages), | ||
| total: $total, | ||
| limit: $filter->getLimit(), | ||
| lastId: $filter->getLastId(), | ||
| ); | ||
| } | ||
|
|
||
| private function loadPageData(array $result): SubscribePage | ||
| { | ||
| /** @var SubscribePage $page */ | ||
| $page = array_shift($result)['page']; | ||
|
|
||
| $data = []; | ||
| foreach ($result as $row) { | ||
| if (isset($row['data'])) { | ||
| $data[] = $row['data']; | ||
| } | ||
| } | ||
| $page->setData($data); | ||
|
|
||
| return $page; | ||
| } | ||
|
Comment on lines
+92
to
106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🩹 Suggested fix — keep the whole result set when collecting `data`- private function loadPageData(array $result): SubscribePage
- {
- /** `@var` SubscribePage $page */
- $page = array_shift($result)['page'];
-
- $data = [];
- foreach ($result as $row) {
- if (isset($row['data'])) {
- $data[] = $row['data'];
- }
- }
- $page->setData($data);
-
- return $page;
- }
+ private function loadPageData(array $result): SubscribePage
+ {
+ /** `@var` SubscribePage $page */
+ $page = $result[0]['page'];
+
+ $data = [];
+ foreach ($result as $row) {
+ if (!empty($row['data'])) {
+ $data[] = $row['data'];
+ }
+ }
+ $page->setData($data);
+
+ return $page;
+ }🤖 Prompt for AI Agents |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,19 +10,21 @@ | |
| use PhpList\Core\Domain\Subscription\Model\SubscribePageData; | ||
| use PhpList\Core\Domain\Subscription\Repository\SubscriberPageDataRepository; | ||
| use PhpList\Core\Domain\Subscription\Repository\SubscriberPageRepository; | ||
| use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
| use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
|
||
| class SubscribePageManager | ||
| { | ||
| public function __construct( | ||
| private readonly SubscriberPageRepository $pageRepository, | ||
| private readonly SubscriberPageDataRepository $pageDataRepository, | ||
| private readonly EntityManagerInterface $entityManager, | ||
| private readonly TranslatorInterface $translator, | ||
| ) { | ||
| } | ||
|
|
||
| public function findPage(int $id): ?SubscribePage | ||
| { | ||
| return $this->pageRepository->findPageWithData($id); | ||
| } | ||
|
|
||
| public function createPage(string $title, bool $active = false, ?Administrator $owner = null): SubscribePage | ||
| { | ||
| $page = new SubscribePage(); | ||
|
|
@@ -35,15 +37,32 @@ public function createPage(string $title, bool $active = false, ?Administrator $ | |
| return $page; | ||
| } | ||
|
|
||
| public function getPage(int $id): SubscribePage | ||
| public function syncPageData(array $data, SubscribePage $page): void | ||
| { | ||
| /** @var SubscribePage|null $page */ | ||
| $page = $this->pageRepository->find($id); | ||
| if (!$page) { | ||
| throw new NotFoundHttpException($this->translator->trans('Subscribe page not found')); | ||
| $existingPageData = []; | ||
| foreach ($this->getPageData($page) as $pageData) { | ||
| $existingPageData[$pageData->getName()] = $pageData; | ||
| } | ||
|
|
||
| return $page; | ||
| foreach ($data as $pageDataKey => $value) { | ||
| if (isset($existingPageData[$pageDataKey])) { | ||
| $pageData = $existingPageData[$pageDataKey]; | ||
| $pageData->setData($value); | ||
| unset($existingPageData[$pageDataKey]); | ||
| continue; | ||
| } | ||
|
|
||
| $pageData = (new SubscribePageData()) | ||
| ->setId($page->getId()) | ||
| ->setName($pageDataKey) | ||
| ->setData($value); | ||
|
|
||
| $this->pageDataRepository->persist($pageData); | ||
|
Comment on lines
+55
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
🩹 Suggested fix — mirror the existing cast $pageData = (new SubscribePageData())
- ->setId($page->getId())
+ ->setId((int)$page->getId())
->setName($pageDataKey)
->setData($value);Quick check on #!/bin/bash
ast-grep --pattern $'class SubscribePageData {
$$$
public function setId($_) { $$$ }
$$$
}'🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| foreach ($existingPageData as $pageData) { | ||
| $this->entityManager->remove($pageData); | ||
| } | ||
| } | ||
|
|
||
| public function updatePage( | ||
|
|
@@ -78,7 +97,7 @@ public function deletePage(SubscribePage $page): void | |
| /** @return SubscribePageData[] */ | ||
| public function getPageData(SubscribePage $page): array | ||
| { | ||
| return $this->pageDataRepository->getByPage($page,); | ||
| return $this->pageDataRepository->getByPage($page); | ||
| } | ||
|
|
||
| public function setPageData(SubscribePage $page, string $name, ?string $value): SubscribePageData | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grouping in
getFilteredAfterIddouble-buckets rows and can crashloadPageData.Every row is appended twice into
$grouped: once under$page->getId()with the full row, and once under$data->getId()with a['data' => ...]-only entry. BecauseSubscribePageData::setId(...)is called with$page->getId()elsewhere (seeSubscribePageManager::setPageData/syncPageData),$data->getId()collides with$page->getId(), so every page's bucket ends up with duplicateddataitems (eachSubscribePageDatagets counted twice).And if
data.idever doesn't equalpage.id, the second branch creates a group that has no'page'key, andloadPageData()'sarray_shift($result)['page']then triggers an undefined-index access on a non-page entry.🩹 Suggested rewrite — single bucket per page, aggregate data inline
📝 Committable suggestion
🤖 Prompt for AI Agents