Skip to content

Commit

Permalink
Merge pull request #5 from davidgorges/chore/postgres-compatibility
Browse files Browse the repository at this point in the history
postgresql compatibility
  • Loading branch information
manuxi authored Aug 30, 2024
2 parents ba34ea2 + bf4e1e9 commit d221aa1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 41 deletions.
100 changes: 61 additions & 39 deletions src/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,24 @@ public static function createEnabledCriteria(): Criteria

public function findAllScheduledEvents(int $limit)
{
$query = $this->createQueryBuilder('e')
->where('e.enabled = 1 AND (e.startDate >= :now OR (e.endDate IS NOT NULL AND e.endDate >= :now))')
->orderBy("e.startDate", "ASC")
$now = new \DateTimeImmutable();
$queryBuilder = $this->createQueryBuilder('e')
->where('e.enabled = :enabled')
->andWhere(
$queryBuilder->expr()->orX(
$queryBuilder->expr()->gte('e.startDate', ':now'),
$queryBuilder->expr()->andX(
$queryBuilder->expr()->isNotNull('e.endDate'),
$queryBuilder->expr()->gte('e.endDate', ':now')
)
)
)
->orderBy('e.startDate', 'ASC')
->setMaxResults($limit)
->setParameter("now", (new \DateTimeImmutable())->format("Y-m-d"));
return $query->getQuery()->getResult();
->setParameter('enabled', 1)
->setParameter('now', $now->format('Y-m-d'));

return $queryBuilder->getQuery()->getResult();
}

/**
Expand All @@ -127,27 +139,27 @@ public function findAllScheduledEvents(int $limit)
* @noinspection PhpMissingReturnTypeInspection
* @noinspection PhpMissingParamTypeInspection
*/
/* public function findByFilters(
$filters,
$page,
$pageSize,
$limit,
$locale,
$options = [],
?UserInterface $user = null,
$entityClass = null,
$entityAlias = null,
$permission = null
) {
$entities = $this->parentFindByFilters($filters, $page, $pageSize, $limit, $locale, $options);
return \array_map(
function (Event $entity) use ($locale) {
return $entity->setLocale($locale);
},
$entities
);
}*/
/* public function findByFilters(
$filters,
$page,
$pageSize,
$limit,
$locale,
$options = [],
?UserInterface $user = null,
$entityClass = null,
$entityAlias = null,
$permission = null
) {
$entities = $this->parentFindByFilters($filters, $page, $pageSize, $limit, $locale, $options);
return \array_map(
function (Event $entity) use ($locale) {
return $entity->setLocale($locale);
},
$entities
);
}*/

protected function appendJoins(QueryBuilder $queryBuilder, $alias, $locale): void
{
Expand All @@ -166,9 +178,9 @@ protected function append(QueryBuilder $queryBuilder, string $alias, string $loc
{

$queryBuilder->andWhere($alias . '.enabled = true');
/* $queryBuilder->andWhere('('. $alias .'.startDate >= :now OR ('. $alias .'.endDate IS NOT NULL AND '. $alias .'.endDate >= :now))');
$queryBuilder->setParameter("now", (new Datetime())->format("Y-m-d H:i:s"));
$queryBuilder->orderBy($alias . ".startDate", "ASC");*/
/* $queryBuilder->andWhere('('. $alias .'.startDate >= :now OR ('. $alias .'.endDate IS NOT NULL AND '. $alias .'.endDate >= :now))');
$queryBuilder->setParameter("now", (new Datetime())->format("Y-m-d H:i:s"));
$queryBuilder->orderBy($alias . ".startDate", "ASC");*/

return [];
}
Expand Down Expand Up @@ -199,23 +211,33 @@ function (Event $entity) use ($locale) {

public function getActiveEvents(array $filters, string $locale, ?int $page, $pageSize, $limit = null, array $options): array
{
$pageCurrent = (key_exists('page', $options)) ? (int)$options['page'] : 0;
// Determine the current page
$pageCurrent = array_key_exists('page', $options) ? (int) $options['page'] : 0;

// Initialize the query builder
$queryBuilder = $this->createQueryBuilder('event')
->leftJoin('event.translations', 'translation')
->where('event.enabled = 1')
->andWhere('translation.locale = :locale')->setParameter('locale', $locale)
->orderBy('event.startDate', 'DESC')
->setMaxResults($limit)
->setFirstResult($pageCurrent * $limit);
->where('event.enabled = :enabled')
->andWhere('translation.locale = :locale')
->setParameter('enabled', 1)
->setParameter('locale', $locale)
->orderBy('event.startDate', 'DESC');

// Apply limit and pagination
if ($limit !== null) {
$queryBuilder->setMaxResults($limit);
}
if ($pageCurrent !== null && $limit !== null) {
$queryBuilder->setFirstResult($pageCurrent * $limit);
}

// Apply additional filters
$this->prepareFilter($queryBuilder, $filters);

// Execute the query and return results
$events = $queryBuilder->getQuery()->getResult();
if (!$events) {
return [];
}
return $events;

return $events ?: [];
}

private function prepareFilter(QueryBuilder $queryBuilder, array $filters): void
Expand Down
4 changes: 2 additions & 2 deletions src/Repository/EventTranslationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function findMissingLocaleByIds(array $ids, string $missingLocale, int $c
{
$query = $this->createQueryBuilder('et')
->addCriteria($this->createIdsInCriteria($ids))
->groupby('et.event')
->having('eventCount < :countLocales')
->groupBy('et.event, et.locale')
->having('COUNT(et.event) < :countLocales')
->setParameter('countLocales', $countLocales)
->andHaving('et.locale = :locale')
->setParameter('locale', $missingLocale)
Expand Down

0 comments on commit d221aa1

Please sign in to comment.